Link to home
Start Free TrialLog in
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.
Avatar of Scott Pletcher
Scott Pletcher
Flag of United States of America image

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
Avatar of sbornstein2
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
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Oh wait sorry it is right I think
How is that example you have taking into account the positive Amount?
thank submitting a new question as well