计数,在案例/演员之后分组

问题描述 投票:0回答:1

我有以下脚本(SQL Server 2016),它正在逐行输出(我可以用 Excel 计算 - 作弊)但我真的很想总结每个“取消”、“firstfill”、“笔芯”等,但我一辈子都无法弄清楚这个 Count / Group by ..

非常感谢任何帮助。抱歉格式问题,无法弄清楚。

Select rx.Storeid, Case When Cast(canceldate as Date) is not null then 'Cancel' When OrigRxNum = RxNum and Status=1 and Canceldate is null then 'FirstFill'
    When OrigRxNum <> RxNum and Status=1 and Canceldate is null then 'Refill'
    When Cast(Canceldate as date) is null and Status=3 then 'Unfill'
    --When OrigRxNum = RxNum and Status=2 then 'Unfill Cancel'
            Else 'Other'
        End RxStatus
From Rx
Where Storeid =575 and Cast(TransactionDate as DATE) >= '02-28-2023' and Cast(TransactionDate as DATE) <= '03-28-2023'
and status in(1,2,3)
sql-server tsql group-by casting case
1个回答
0
投票

只需将该查询包含在计数中?

SELECT x.RxStatus,
COUNT(*) AS Frequency
(Select rx.Storeid, 
Case 
    When Cast(canceldate as Date) is not null then 'Cancel' 
    When OrigRxNum = RxNum and Status=1 and Canceldate is null then 'FirstFill'
    When OrigRxNum <> RxNum and Status=1 and Canceldate is null then 'Refill'
    When Cast(Canceldate as date) is null and Status=3 then 'Unfill'
    --When OrigRxNum = RxNum and Status=2 then 'Unfill Cancel'
            Else 'Other'
End RxStatus
From Rx
Where Storeid =575 and Cast(TransactionDate as DATE) >= '02-28-2023' and Cast(TransactionDate as DATE) <= '03-28-2023'
and status in(1,2,3)) x
GROUP BY x.RxStatus;
© www.soinside.com 2019 - 2024. All rights reserved.