开发者

How to join two query in SQL (Oracle)

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 RCT开发者_运维百科AMOUNT>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


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


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


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


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

can also be useful.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜