troubleshooting Question

SQL Syntax Grouping Sum question

Avatar of sbornstein2
sbornstein2 asked on
Microsoft SQL ServerSQL
7 Comments1 Solution144 ViewsLast Modified:
Hello all,

I have the following example data:

Table:  Customer
CustomerID   CustomerDate   CustomerCode    
1                      3/15/2017           ABC                      
2                      3/15/2017           ABC                      
3                      3/16/2017           DEF                        
4                      3/16/2017           DEF                        

Table: CustomerDetail
CustomerDetailID       CustomerID       CurrencyType       Amount
20                                  1                           EUR                        100
21                                  1                           CAD                        200
22                                  2                           EUR                        125
23                                  2                           CAD                        150
24                                  3                           USD                        100
25                                  3                           CAD                        -200
26                                  4                           USD                        75
24                                  4                           CAD                        50

What I need in a result set is first I need to group together the Customer table by CustomerDate and CustomerCode.   Then I need to get the grouped CustomerID's and create two sum records summing the amount per currency across the CustomerID's for each grouping detail records.   So in the above scenario my return results would be:

CustomerDate       CustomerCode      CurrencyType        AmountSum
3/15/2017              ABC                          EUR                          225
3/15/2017              ABC                          CAD                          350
3/16/2017              DEF                          USD                          175
3/16/2017              DEF                          CAD                         -150

What is the best way to handle this with the grouping.
Join our community to see this answer!
Unlock 1 Answer and 7 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 7 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