tags:

views:

20

answers:

3

How can I join these queries?

SELECT RCTDT, SUM(RCTAMOUNT), COUNT(RCTAMOUNT) FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009' 
AND RCTTYPE='CA' AND RCTAMOUNT>0
GROUP BY RCTDT

---

SELECT RCTDT, SUM(RCTAMOUNT), COUNT(RCTAMOUNT) FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009' 
AND RCTTYPE='CQ' AND RCTAMOUNT>0
GROUP BY RCTDT
+1  A: 

Use the IN operator:

SELECT RCTDT, SUM(RCTAMOUNT), COUNT(RCTAMOUNT) FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009' 
AND RCTTYPE IN ('CA','CQ') AND RCTAMOUNT>0
GROUP BY RCTDT
cletus
A: 

In this particular example, use an IN as suggested by cletus. In other cases, where the two result sets cannot be generated from the same select, use a UNION to combine the results:

SELECT something FROM somewhere
UNION
SELECT something FROM somewhere_else
Jørn Schou-Rode
+1  A: 

Maybe you meant this

SELECT RCTDT,
 SUM(case when rcttype='CA' then RCTAMOUNT else null end) as CASUM, 
 COUNT(case when rcttype='CA' then RCTAMOUNT else null end) as CACOUNT,
 SUM(case when rcttype='CQ' then RCTAMOUNT else null end) as CQSUM, 
 COUNT(case when rcttype='CQ' then RCTAMOUNT else null end) as CQCOUNT,
FROM RECEIPTS4
WHERE RCTDT BETWEEN '01-nov-2009' AND '30-nov-2009' 
AND RCTTYPE in('CA','CQ') AND RCTAMOUNT>0
GROUP BY RCTDT
josephj1989