The query below works correctly if I do not add a where clause for date in the history table but once I add it records are skipped from the customer table which is set to a left outer join. My record custnums are sequential so I know that some are being skipped. I am trying to pull a customers balance forward even if there was not activity for a time period. Here is my query:
Any help will be greatly appreciated.
Thanks in advance.
Code:
SELECT c.CUSTNUM AS CustNum, c.CUSTNAME AS CustName, c.BALFWD AS BalFwd, SUM(CASE WHEN h.transtype = '2' AND h.recordflag = 'B' THEN h.dollars ELSE 0 END) AS AdjTicketCash, SUM(CASE WHEN h.transtype = '1' AND h.recordflag = 'B' THEN h.dollars ELSE 0 END) AS AdjTicketChrg, SUM(CASE WHEN h.transtype = '1' AND h.recordflag IN ('A', 'B') THEN h.dollars ELSE 0 END) AS ChrgTicketTot, SUM(CASE WHEN h.recordflag = 'P' THEN (- 1 * h.dollars) ELSE 0 END) AS Payment, c.BALFWD+SUM(CASE WHEN h.transtype = '2' AND h.recordflag = 'B' THEN h.dollars ELSE 0 END)+ SUM(CASE WHEN h.transtype = '1' AND h.recordflag IN ('A', 'B') THEN h.dollars ELSE 0 END)+ SUM(CASE WHEN h.recordflag = 'P' THEN (- 1 * h.dollars) ELSE 0 END) as EndBalance
FROM Customer AS c LEFT OUTER JOIN History AS h ON c.CUSTNUM = h.CUSTNUM
WHERE h.DATEIN >= '2008-10-01' and h.DATEIN <= '2008-10-31'
GROUP BY c.CUSTNUM, c.custname, c.BALFWD
ORDER BY c.CustNum
Any help will be greatly appreciated.
Thanks in advance.