如何在 SQL (Oracle) 中连接两个查询

发布于 2024-09-03 19:53:42 字数 381 浏览 5 评论 0原文

我如何加入这些查询?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寄人书 2024-09-10 19:53:42

使用 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

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
相思碎 2024-09-10 19:53:42

也许你的意思是这个

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

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
唯憾梦倾城 2024-09-10 19:53:42

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

SELECT something FROM somewhere
UNION
SELECT something FROM somewhere_else

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
一身骄傲 2024-09-10 19:53:42

从收据中选择 RCTDT、SUM(RCTAMOUNT)、COUNT(RCTAMOUNT)4
“2009 年 11 月 1 日”和“2009 年 11 月 30 日”之间的 RCTDT 位置
AND RCTTYPE IN ('CA','CQ') 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 IN ('CA','CQ') AND RCTAMOUNT>0
GROUP BY RCTDT

can also be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文