I am using following query which works fine for me except one problem
SELECT f.period AS month,
SUM(p.revenue * ((100-q.rate)/100)) AS revenue,
COUNT(DISTINCTq.label) AS tot_stmt
FROM files f, reports p, rates q,albums a
WHERE f.period IN ('2010-06-01','2010-05-01','2010-04-01','2010-03-01')
AND f.period_closed = TRUE
AND q.period = f.period
AND a.id = q.album_id
AND p.file_id = f.id
AND p.upc = a.upc
AND p.revenue IS NOT NULL
GROUP BY month
ORDER BY month DESC;
O/P =>
month revenue
tot_stmt2010-06-01 10.00 2
2010-05-01 340.47 2
I want result like following:
month revenue
tot_stmt2010-06-01 10.00 2
2010-05-01 340.47 2
2010-04-01 0.00
0
2010-03-01 0.00
0
Explanations:
JOINclausesCOUNT(column)returns0if all aggregated records have valueNULLin that columnSUM(column)returnsNULLif all aggregated records have valueNULLin that columnp.revenueto beNULL(dropped thatcriteria)
Note
You don’t seem to be getting anything from the albums table so you can take it out.