我正在试图弄清楚如何计算我在此查询中创建的三个组中每个组的实例数量:
SELECT
CASE WHEN idStatus=6 THEN "Accepted"
WHEN idStatus in (7,8) THEN "Denied"
WHEN idStatus in (1,2,3,4,5,9,10,11,12,13,14) THEN "Working" END AS 'Claim Status'
FROM claims
WHERE Date > '2013-01-01'
我想要一个包含两个列的结果,“声明状态”和“计数”,并且下面列出了我的三个组中的每个组,其中包含了表中的计数。
答案 0 :(得分:1)
如果您将查询“包装”在另一个select语句中,那么性能将显着降低。您应按您正在计算的字段进行分组:
SELECT
CASE
WHEN idStatus = 6 THEN "Accepted"
WHEN idStatus IN (7,8) THEN "Denied"
WHEN idStatus IN (1,2,3,4,5,9,10,11,12,13,14) THEN "Working"
END AS ClaimStatus,
COUNT(*) ClaimCount
FROM claims
WHERE Date > '2013-01-01'
GROUP BY ClaimStatus
答案 1 :(得分:0)
使用外部查询包装您的查询以进行分组:
SELECT claim_status AS 'Claim Status', COUNT(*) AS 'Count'
FROM (
SELECT
CASE
WHEN idStatus=6 THEN "Accepted"
WHEN idStatus in (7,8) THEN "Denied"
WHEN idStatus in (1,2,3,4,5,9,10,11,12,13,14) THEN "Working"
END AS claim_status
FROM claims
WHERE Date > '2013-01-01'
} t
GROUP BY claim_status