簡體   English   中英

SQL Distinct Count 和 Case when

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM