SQL Syntax Grouping Sum question

sbornstein2
sbornstein2 used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Scott PletcherSenior DBA
Most Valuable Expert 2018
Top Expert 2014

Commented:
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

Author

Commented:
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
Software Team Lead
Commented:
for your second request, you may customize this:

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

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

Open in new window

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
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

Author

Commented:
Oh wait sorry it is right I think

Author

Commented:
How is that example you have taking into account the positive Amount?

Author

Commented:
thank submitting a new question as well

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial