Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to select data (two columns) from table based on a composite unique value and insert in another...


How to select data (two columns) from table based on a composite unique value and insert in another table?

Author
Message
SQL_beginner1
SQL_beginner1
Grasshopper
Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)Grasshopper (16 reputation)

Group: General Forum Members
Points: 16 Visits: 48
I have a table CustomerMaster and the two fields in it - CustomerID and Source - give me unique values. The table already has a separate PK.

Another table - Contracts - has two fields that map to the two fields in CustomerMaster: Contracts.CustomerNum maps to CustomerMaster.CustomerID, and Contacts.Source maps to CustomerMaster.Source.

I want to get distinct rows from Contracts table, based on the combination of (CustomerNum & Source), and then append those values for CustomerNum and Source in CustomerMaster table (columns CustomerID and Source respectively).

I'm able to use distinct in a query to get the unique records (select distinct (CustomerNum+Source) from Contracts) but I'm at sea as to how to select the values for CustomerNum & Source based on the distinct of (CustomerNum+Source).

Any help will be appreciated.
Gazareth
Gazareth
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2872 Visits: 5351
Hi,
Think this is what you're after, if I've read it right:


SELECT c.CustomerNum, c.Source, COUNT(cm.*)
FROM Contracts c
INNER JOIN CustomerMaster cm ON cm.CustomerID = c.CustomerNum AND cm.Source = c.Source
GROUP BY c.CustomerNum, c.Source



Cheers
Gaz
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search