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.
sbornstein2Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
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
0
sbornstein2Author 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
0
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

sbornstein2Author 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
0
sbornstein2Author Commented:
Oh wait sorry it is right I think
0
sbornstein2Author Commented:
How is that example you have taking into account the positive Amount?
0
sbornstein2Author Commented:
thank submitting a new question as well
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.