简体   繁体   English

SQL Distinct Count 和 Case when

[英]SQL Distinct Count and Case when

Here is my query for Microsoft SQL Server:这是我对 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]
)

Apparently there is issues with the brackets.显然括号有问题。 Any help is much appreciated!任何帮助深表感谢! :) :)

As already mentioned in the comments, you need an alias for your subquery.正如评论中已经提到的,您的子查询需要一个别名。

change this改变这个

count(distinct AEN) as Customers  

into this进入这个

count(distinct t.AEN) as Customers  

and also this还有这个

    [table]
)

into this进入这个

    [table]
) t

look at this DBFiddle看看这个DBFiddle

This will run without the error, but I have no clue if this is what you want to achieve, your question is not any clear about that这将在没有错误的情况下运行,但我不知道这是否是您想要实现的目标,您的问题对此并不清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM