Avatar of sbornstein2
sbornstein2
 asked on

SQL Syntax Grouping Sum question

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.
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
sbornstein2

8/22/2022 - Mon
Scott Pletcher

SELECT C.CustomerDate, C.CustomerCode, CD.Currency_Type, SUM(CD.Amount) AS AmountSum
FROM CustomerDetail CD
INNER JOIN Customer C ON C.CustomerID = CD.CustomerID
GROUP BY C.CustomerDate, C.CustomerCode, CD.Currency_Type
ORDER BY C.CustomerDate, C.CustomerCode, CD.Currency_Type
sbornstein2

ASKER
Thanks Scott can I add one more wrinkle to the fold lets say I have the same scenario with one added field.  Once I net the two currencies I need to take the positive currency back to the Customer table and use that Identifier on both records.  So in the below case I have a positive CAD 275 I want to apply the 5678 as identifier to both records.  

Table:  Customer
CustomerID   CustomerDate   CustomerCode      Identifier
1                      3/15/2017           ABC                          1234
2                      3/15/2017           ABC                          5678


Table: CustomerDetail
CustomerDetailID       CustomerID       CurrencyType       Amount
20                                  1                           EUR                        100
21                                  1                           EUR                        -200
22                                  2                           CAD                        125
23                                  2                           CAD                        150

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    Identifier
3/15/2017              ABC                          EUR                          -100                   5678
3/15/2017              ABC                          CAD                          275                    5678
ASKER CERTIFIED SOLUTION
Ryan Chong

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
sbornstein2

ASKER
Think almost there.  I just ran this example and the Identifier should be the 1234 because that Net is the positive value.

Declare @Customer Table
(
      CustomerID int,
      CustomerDate date,
      CustomerCode varchar(30),
      Identifier int
)
Insert into @Customer values
(1, '3/15/2017', 'ABC', 5671),
(2, '3/15/2017', 'ABC', 1234);

Declare @CustomerDetail Table
(
      CustomerDetailID int,
      CustomerID int,
      CurrencyType varchar(3),
      Amount money
)
Insert into @CustomerDetail values
(20, 1, 'EUR', 100),
(21, 1, 'EUR', -200),
(22, 2, 'CAD', 125),
(23, 2, 'CAD', 150);

;With cte as
(
Select a.CustomerDate, a.CustomerCode, b.CurrencyType, Sum(b.Amount) AmountSum, --max(a.Identifier) Identifier,
max(a.Identifier) over (partition by a.CustomerDate, a.CustomerCode) Identifier,
row_number() over (partition by a.CustomerDate, a.CustomerCode order by a.CustomerID) idx
from @Customer a inner join @CustomerDetail b
on a.CustomerID = b.CustomerID
Group By a.CustomerDate, a.CustomerCode, b.CurrencyType, a.Identifier, a.CustomerID
)
Select a.CustomerDate, a.CustomerCode, a.CurrencyType, Sum(a.AmountSum) AmountSum, a.Identifier
from cte a
group by a.CustomerDate, a.CustomerCode, a.CurrencyType, a.Identifier, a.idx
order by a.CustomerDate, a.CustomerCode, a.idx
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
sbornstein2

ASKER
Oh wait sorry it is right I think
sbornstein2

ASKER
How is that example you have taking into account the positive Amount?
sbornstein2

ASKER
thank submitting a new question as well
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.