dbTalk Databases Forums  

Query to get distinct records

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Query to get distinct records in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Sam
 
Posts: n/a

Default Query to get distinct records - 06-29-2005 , 01:16 PM






I have 2 tables
DistinctVend and NonRefvend. Both of them have the VendorID column.

The DistinctVend has 25000 records
The NonRefvend table has 26000 records.

I want to run a query that will select only the records from NonRefVend
table where the Vendorid is also in the DistinctVend table.

Furthermore I want to be able to document the vendorid's that are in
variance in the two tables.

Could someone assist please.

Thanks
S Commar
s_commar (AT) hotmail (DOT) com



Reply With Quote
  #2  
Old   
Keith Kratochvil
 
Posts: n/a

Default Re: Query to get distinct records - 06-29-2005 , 01:33 PM






Hopefully this is what you are looking for:


/*
Vendorid exists in both tables
*/
SELECT A.*
FROM NonRefVend A
JOIN DistinctVend B ON A.Vendorid = B.VendorID

/*
Vendorid only in NonRefVend
*/
SELECT A.*
FROM NonRefVend A
WHERE NOT EXISTS (SELECT * FROM DistinctVend B WHERE A.Vendorid =
B.VendorID)

/*
Vendorid only in DistinctVend
*/
SELECT A.*
FROM DistinctVend A
WHERE NOT EXISTS (SELECT * FROM NonRefVend B WHERE A.Vendorid = B.VendorID)



--
Keith


"Sam" <s_commar (AT) hotmail (DOT) com> wrote

Quote:
I have 2 tables
DistinctVend and NonRefvend. Both of them have the VendorID column.

The DistinctVend has 25000 records
The NonRefvend table has 26000 records.

I want to run a query that will select only the records from NonRefVend
table where the Vendorid is also in the DistinctVend table.

Furthermore I want to be able to document the vendorid's that are in
variance in the two tables.

Could someone assist please.

Thanks
S Commar
s_commar (AT) hotmail (DOT) com




Reply With Quote
  #3  
Old   
Narayana Vyas Kondreddi
 
Posts: n/a

Default Re: Query to get distinct records - 06-29-2005 , 01:37 PM



SELECT a.VendorID
FROM NonRefVend AS a
WHERE EXISTS
(
SELECT 1
FROM DistinctVend AS b
WHERE b.VendorID = a.VendorID
)

This will give you a list of all vendor ids from NonRefVend that exist in
DistinctVend table.

Regarding your second request, could you please elaborate pn 'variance'?
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @ http://vyaskn.tripod.com/


"Sam" <s_commar (AT) hotmail (DOT) com> wrote

Quote:
I have 2 tables
DistinctVend and NonRefvend. Both of them have the VendorID column.

The DistinctVend has 25000 records
The NonRefvend table has 26000 records.

I want to run a query that will select only the records from NonRefVend
table where the Vendorid is also in the DistinctVend table.

Furthermore I want to be able to document the vendorid's that are in
variance in the two tables.

Could someone assist please.

Thanks
S Commar
s_commar (AT) hotmail (DOT) com





Reply With Quote
  #4  
Old   
Sandeep Commar
 
Posts: n/a

Default Re: Query to get distinct records - 06-29-2005 , 05:19 PM




Hi

Thanks for your help. I wanted to get a list of all the vendorid that do
not match in both the tables.

Thanks
S Commar


*** Sent via Developersdex http://www.developersdex.com ***

Reply With Quote
  #5  
Old   
Narayana Vyas Kondreddi
 
Posts: n/a

Default Re: Query to get distinct records - 06-29-2005 , 05:33 PM



Have you checked Keith's response?
--
Vyas, MVP (SQL Server)
SQL Server Articles and Code Samples @ http://vyaskn.tripod.com/


"Sandeep Commar" <s_commar (AT) hotmail (DOT) com> wrote

Quote:
Hi

Thanks for your help. I wanted to get a list of all the vendorid that do
not match in both the tables.

Thanks
S Commar


*** Sent via Developersdex http://www.developersdex.com ***



Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.