[英]SQL Distinct Count and Case when
这是我对 Microsoft SQL Server 的查询:
select count(distinct AEN) as Customers
from
( select top 100 ERDAT, AEN,
case
when AEN = 'MBLE' AND ERDAT between '20220401' and '20220430' then 'Mobile'
when AEN = 'ISU' AND ERDAT between '20220401' and '20220430' then 'Website'
when AEN = 'CRM' AND ERDAT between '20220401' and '20220430' then 'CRM'
when AEN like '_[0-9]%' AND ERDAT between '20220401' and '20220430' then 'ID Number'
else 'Others'
end as 'Channels'
from [Table]
)
显然括号有问题。 任何帮助深表感谢! :)
正如评论中已经提到的,您的子查询需要一个别名。
改变这个
count(distinct AEN) as Customers
进入这个
count(distinct t.AEN) as Customers
还有这个
[table]
)
进入这个
[table]
) t
看看这个DBFiddle
这将在没有错误的情况下运行,但我不知道这是否是您想要实现的目标,您的问题对此并不清楚
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.