troubleshooting Question

SQL Query Grouping Question Record Inserts

Avatar of sbornstein2
sbornstein2 asked on
Microsoft SQL ServerSQL
9 Comments1 Solution136 ViewsLast Modified:
Hello all,

Looking for a clean way to do the following little tricky.

Table:  Customer
CustomerDate     CustomerName     CustomerCode    CustomerLocID    Quantity
3/10/17                 ABC Corp                 CDO                      345XCC                  100
3/10/17                 ABC Corp                 CDO                      345XCC                  300
3/10/17                 ABC Corp                 CDO                      789UNN                120
3/10/17                 ABC Corp                 CDO                      789UNN                  85

I have two tables I need to do the following:

Table:  CustomerRoot
CustomerID    CustomerDate     CustomerName    CustomerCode
(Identity Col)

Table:  CustomerDetail
CustomerID   CustomerLocID   Quantity

I need to group the records above by the Customer fields  record with just these fields:
CustomerDate     CustomerName     CustomerCode    CustomerLocID

Gather those grouped sets then create one CustomerRoot record and two CustomerDetail records per CustomerLocID set.   There is a reason I can't add the CustomerLocID to the CustomerRoot table some special circumstance I am dealing with these tables the CustomerRoot record won't be unique other than the Identity CustomerID.   It is like I need to get the detail records together grouped then create the Root and Detail records.   Example ResultSet:

Table:  CustomerRoot - only want these fields in this root table
CustomerID    CustomerDate     CustomerName    CustomerCode
1                       3/10/17                  ABC Corp               CDO
2                       3/10/17                  ABC Corp               CDO


Table:  CustomerDetail
CustomerID   CustomerLocID   Quantity
1                      345XCC                 100
1                      345XCC                 300
2                      789UNN               120
2                      789UNN                 85

Thanks for any help all.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 9 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros