I've created a table of data and need to update the [debit] amount where the description is 'MM House' as follows:
When [Description] = 'MM House'
sum ('MM House' [Amount] - 'Customer Charge 1'[ Amount] - 'Customer Charge 2' [Amount] - 'Customer Charge 3' [Amount]) AS Debit
Group by DocDate, DocNumber
In addition, if the calculation is <= 0 set Debit to 0 and there could be up to 12 Customer Charges per document.
,CASE
WHEN [Description] = 'MM House'
THEN Amount - SUM(CASE WHEN [Description] = 'Customer Charge' THEN Amount END) OVER (PARTITION BY DocNumber, CustID)
ELSE Debit
END AS Debit
Thanks so much! The suggestion works but it does lead me to another question since it inserts the data in an additional column. Following are the results:
DocNumber
CustID
Amount
CustChargeSum
Debit
DocDate
Description
CustChargeSum
115003
1150
242.18
NULL
242.18
2/19/2020
Customer Charge
242.18
115003
1150
242.18
NULL
242.18
2/19/2020
MM House
0.00
115004
1150
643.78
NULL
643.78
2/20/2020
Customer Charge
643.78
115004
1150
214.24
NULL
214.24
2/20/2020
Customer Charge
214.24
115004
1150
424.00
NULL
424.00
2/20/2020
Customer Charge
424.00
115004
1150
1,282.02
NULL
1,282.02
2/20/2020
MM House
0.00
Can I now update the Debit column amount with the results? The below attempt fails because it is a windowed function.
update SRR_AR_SalesDataLoad set Debit =
(CASE
WHEN [Description] = 'MM House'
THEN Amount - SUM(CASE WHEN [Description] = 'Customer Charge' THEN Amount END) OVER (PARTITION BY DocNumber, CustID)
ELSE Debit
END)
Thanks for the response jeffw8713. If the source data were correct MM House amount would always equal the sum of the Customer Charge amounts and MM House could be eliminated. However, there are three possible scenarios identified so far:
MM House amount equals the sum of the Customer Charge amounts. In this case MM House amount is a duplicate and should be eliminated.
MM House amount is greater than the sum of customer charge amounts. In this case MM House amount should be MM House amount less sum of customer charge amounts. This data indicates that there is a Customer Charge missing from the source data.
MM House amount is less than the sum of customer charge amounts. The result will be a negative MM House amount.
WITH Calcs
AS
(
SELECT DocNumber, CustID, CustChargeSum
,CASE
WHEN [Description] = 'MM House'
THEN Amount - SUM(CASE WHEN [Description] = 'Customer Charge' THEN Amount END) OVER (PARTITION BY DocNumber, CustID)
ELSE Debit
END AS NewCustChargeSum
FROM <YourTable, sysname, YourTable>
)
UPDATE Calcs
SET CustChargeSum = NewCustChargeSum;
Thanks once again Ifor. I'm getting a syntax error on the AS. I'm not familiar with the 'With' function but am researching to get an understanding and consequently determine the issue.
Thank you and that takes care of the AS syntax error but now have syntax error near FROM. I have not figured out what the sysname refers to in the suggested code.
Got it! Thanks all for sharing your knowledge. The below tests as desired.
;WITH NewCustChargeSum
AS
(
SELECT DocNumber, CustID, CustChargeSum, DocDate
,CASE
WHEN [Description] = 'MM House'
THEN Amount - SUM(CASE WHEN [Description] = 'Customer Charge' THEN Amount END) OVER (PARTITION BY DocNumber, CustID, DocDate)
ELSE Debit
END AS NewCustChargeSUm
FROM SRR_AR_SalesDataLoad
where CustID = '1198'