gpt4 book ai didi

sql - 一行sql中的多个where子句

转载 作者:行者123 更新时间:2023-12-05 00:00:01 27 4
gpt4 key购买 nike

我想采用以下语句并将其融合为一个查询。

SELECT COUNT(*) AS count1 WHERE Month='11' AND Flag = 1
SELECT COUNT(*) AS count2 WHERE Month='11' AND Flag = 2
SELECT COUNT(*) AS count1 WHERE Month='12' AND Flag = 1
SELECT COUNT(*) AS count2 WHERE Month='12' AND Flag = 2

我希望它显示为一个查询,其中包含 count1 和 count2 列以及第 11 个月和第 12 个月的行。

这有语法吗?

最佳答案

您可以组合 SUMCASE一次性获得各种计数:

SELECT
Month,
SUM(CASE WHEN Flag=1 THEN 1 ELSE 0 END) as count1,
SUM(CASE WHEN Flag=2 THEN 1 ELSE 0 END) as count2
from
...
WHERE Month in ('11','12')
GROUP BY
Month /* Other columns? */

关于sql - 一行sql中的多个where子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10916042/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com