SQL跨一个表中的多列计数

时间:2018-11-08 13:53:50

标签: sql sql-server case

我一直试图基于状态ID(活动)和依赖类型来获取特定种群的计数。不幸的是,相关项在多个列中列出,例如相关项1,相关项2等。

我试图利用嵌套的case语句,然后对结果求和:

select TOP (100) [Month],
    [SCHEME CODE DESCRIPTION],
    [MEMBER NUMBER],
    Sum(CASE
        WHEN ([DEPENDANT DEPENDANT- TYPE 2] in ('A','M','O','S') and [DEPENDANT STATUS 2] = 'Active')
        THEN 1 
        WHEN ([DEPENDANT DEPENDANT- TYPE 3] in ('A','M','O','S') and [DEPENDANT STATUS 3] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 4] in ('A','M','O','S') and [DEPENDANT STATUS 4] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 5] in ('A','M','O','S') and [DEPENDANT STATUS 5] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 6] in ('A','M','O','S') and [DEPENDANT STATUS 6] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 7] in ('A','M','O','S') and [DEPENDANT STATUS 7] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 8] in ('A','M','O','S') and [DEPENDANT STATUS 8] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 9] in ('A','M','O','S') and [DEPENDANT STATUS 9] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 10] in ('A','M','O','S') and [DEPENDANT STATUS 10] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 11] in ('A','M','O','S') and [DEPENDANT STATUS 11] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 12] in ('A','M','O','S') and [DEPENDANT STATUS 12] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 13] in ('A','M','O','S') and [DEPENDANT STATUS 13] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 14] in ('A','M','O','S') and [DEPENDANT STATUS 14] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 15] in ('A','M','O','S') and [DEPENDANT STATUS 15] = 'Active')
        THEN 1
        WHEN ([DEPENDANT DEPENDANT- TYPE 16] in ('A','M','O','S') and [DEPENDANT STATUS 16] = 'Active')
        THEN 1
        ELSE 0
    END) as NUM_ADULT_DEP
from [PROFMED].[dbo].[Members_Data]
group by [Month],
            [SCHEME CODE DESCRIPTION],
            [MEMBER NUMBER]
order by NUM_ADULT_DEP desc
;

返回的结果不正确,因为它没有对各列求和。

数据示例:

Data

预期结果: Expected Result

请协助:)

1 个答案:

答案 0 :(得分:3)

我相信您正在尝试执行此操作(基本上,我已将您的一个大案例声明分解为15个并添加了它们):

SUM(
      CASE WHEN [DEPENDANT DEPENDANT- TYPE 2]  IN ('A','M','O','S') AND [DEPENDANT STATUS 2]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 3]  IN ('A','M','O','S') AND [DEPENDANT STATUS 3]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 4]  IN ('A','M','O','S') AND [DEPENDANT STATUS 4]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 5]  IN ('A','M','O','S') AND [DEPENDANT STATUS 5]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 6]  IN ('A','M','O','S') AND [DEPENDANT STATUS 6]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 7]  IN ('A','M','O','S') AND [DEPENDANT STATUS 7]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 8]  IN ('A','M','O','S') AND [DEPENDANT STATUS 8]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 9]  IN ('A','M','O','S') AND [DEPENDANT STATUS 9]  = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 10] IN ('A','M','O','S') AND [DEPENDANT STATUS 10] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 11] IN ('A','M','O','S') AND [DEPENDANT STATUS 11] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 12] IN ('A','M','O','S') AND [DEPENDANT STATUS 12] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 13] IN ('A','M','O','S') AND [DEPENDANT STATUS 13] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 14] IN ('A','M','O','S') AND [DEPENDANT STATUS 14] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 15] IN ('A','M','O','S') AND [DEPENDANT STATUS 15] = 'Active' THEN 1 ELSE 0 END
    + CASE WHEN [DEPENDANT DEPENDANT- TYPE 16] IN ('A','M','O','S') AND [DEPENDANT STATUS 16] = 'Active' THEN 1 ELSE 0 END
) AS NUM_ADULT_DEP

话虽如此,这是一个糟糕的数据库设计。您应该为家属创建一个单独的表。