如何在SQL(Oracle)中加入两个查询

时间:2010-06-03 07:04:09

标签: oracle9i

我如何加入这些查询?

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

4 个答案:

答案 0 :(得分:1)

使用IN运算符:

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

答案 1 :(得分:1)

也许你的意思是

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

答案 2 :(得分:0)

在此特定示例中,使用cletus建议的IN。在其他情况下,如果无法从同一个选择中生成两个结果集,请使用UNION来组合结果:

SELECT something FROM somewhere
UNION
SELECT something FROM somewhere_else

答案 3 :(得分:0)

来自RECEIPTS4的SELECT RCTDT,SUM(RCTAMOUNT),COUNT(RCTAMOUNT) '01 -nov-2009'和'30 -nov-2009'之间的关系 AND RCTTYPE IN('CA','CQ')和RCTAMOUNT> 0 GROUP BY RCTDT

也很有用。