【发布时间】:2018-03-15 13:15:23
【问题描述】:
我正在执行 sql 查询,在其中检索特定年份的 Accounts 列表。我认为下面的代码很好。
SELECT count(DISTINCT ACCOUNT) AS ACCOUNT,
CASE when MONTH(Created)=1 then 'January'
when MONTH(Created)=2 then 'February'
when MONTH(Created)=3 then 'March'
when MONTH(Created)=4 then 'April'
when MONTH(Created)=5 then 'May'
when MONTH(Created)=6 then 'June'
when MONTH(Created)=7 then 'July'
when MONTH(Created)=8 then 'August'
when MONTH(Created)=9 then 'September'
when MONTH(Created)=10 then 'October'
when MONTH(Created)=11 then 'November'
when MONTH(Created)=12 then 'December'
else '3' end AS MONTH
FROM DB_Table
WHERE
AND (DB_Table.Cancelled < '1901-01-01' OR DB_Table Cancelled > '2017-12-31')
AND YEAR(Created)='2017'
GROUP BY MONTH(Created)
Order BY MONTH(Created)
直到我这样检查:
Select count(DISTINCT ACCOUNT) AS ACCOUNT
FROM DB_Table
WHERE
AND (DB_Table.Cancelled < '1901-01-01' OR DB_Table Cancelled > '2017-12-31')
AND YEAR(Created)='2017'
我有两个不同的总和,但 Group by 给我的计数似乎比底部查询高。 有什么建议吗?
【问题讨论】:
-
嗯,你肯定每个帐户/月组合比单独帐户有更多不同的值。除非你只有一个月的数据。明白我的意思吗?
-
是的,可以理解,可以用上面的查询吗?使用诸如拥有左右之类的东西?
-
你想在这里实现什么?您问“是否可以使用上述查询”,what 可能吗?
-
我很抱歉 - 是否可以像使用底部查询一样返回顶部查询的相同计数? “不同”应该适用于全年而不是每个月
-
最简单的方法是使用子查询,但是月份 case 表达式的意义何在?