IanWaterman
Programmer
We have a Balance table which various balances, however, balance is only updated when there is a movement. Previous balance filed has its enddateid updated and a new entry is created with an end date set 100 years in future. Following query finds balance on a specific date. I need to produce a balance query showing balance on each date, including where currently no date exists. Query below works for all dates simply carrying balance forward from when balance last entered.
SELECT AgreementID,
SUM(CASE WHEN fb.AccountTypeBaseCode = '1010' AND fb.AccountTypeStandardCode = '0100' THEN COALESCE(BalanceSAC,0) END) AS SalesLedgerOpenSAC,
SUM(CASE WHEN fb.AccountTypeBaseCode = '3020' AND fb.AccountTypeStandardCode = '0300' THEN COALESCE(BalanceSAC,0) END) AS CurrentAccountOpenSAC,
SUM(CASE WHEN fb.AccountTypeBaseCode = '3010' AND fb.AccountTypeStandardCode = '0300' THEN COALESCE(BalanceSAC,0) END) AS ClientOpeningBalTodaySAC
FROM F_Balance fb
WHERE BalanceStartDateID < @OpeningAccountDateIDVar AND BalanceEndDateID >= @OpeningAccountDateIDVar
AND ( (fb.AccountTypeBaseCode = '1010' AND fb.AccountTypeStandardCode = '0100')
OR (fb.AccountTypeBaseCode = '3020' AND fb.AccountTypeStandardCode = '0300')
OR (fb.AccountTypeBaseCode = '3010' AND fb.AccountTypeStandardCode = '0300') )
GROUP BY AgreementID
I have a date table which links on DateId (D_DatePeriod) and holds all dates.
Fields of interest are Id and ActualDate
I could achieve result I want by unioning data 31 times for each date in a potential month and then filtering using the Dateperiod table
Is there an easier more efficient way by some how cross joining on my date table
Thank you
Ian
SELECT AgreementID,
SUM(CASE WHEN fb.AccountTypeBaseCode = '1010' AND fb.AccountTypeStandardCode = '0100' THEN COALESCE(BalanceSAC,0) END) AS SalesLedgerOpenSAC,
SUM(CASE WHEN fb.AccountTypeBaseCode = '3020' AND fb.AccountTypeStandardCode = '0300' THEN COALESCE(BalanceSAC,0) END) AS CurrentAccountOpenSAC,
SUM(CASE WHEN fb.AccountTypeBaseCode = '3010' AND fb.AccountTypeStandardCode = '0300' THEN COALESCE(BalanceSAC,0) END) AS ClientOpeningBalTodaySAC
FROM F_Balance fb
WHERE BalanceStartDateID < @OpeningAccountDateIDVar AND BalanceEndDateID >= @OpeningAccountDateIDVar
AND ( (fb.AccountTypeBaseCode = '1010' AND fb.AccountTypeStandardCode = '0100')
OR (fb.AccountTypeBaseCode = '3020' AND fb.AccountTypeStandardCode = '0300')
OR (fb.AccountTypeBaseCode = '3010' AND fb.AccountTypeStandardCode = '0300') )
GROUP BY AgreementID
I have a date table which links on DateId (D_DatePeriod) and holds all dates.
Fields of interest are Id and ActualDate
I could achieve result I want by unioning data 31 times for each date in a potential month and then filtering using the Dateperiod table
Is there an easier more efficient way by some how cross joining on my date table
Thank you
Ian