1

我有一个包含原始数据的表,我想从中返回不同的 people_id 计数。如果计数具有不同的分组,我该如何在 CTE 中执行此操作?这是我到目前为止所拥有的:

;with cte as 
    (select 
       program_modifier_id, program_modifier, people_id, group_profile_id, 
       current_status, license_number, is_managing_office,
       program_info, program_name, program_code, group_profile_type_id
     From
    #enrollments en with (nolock) )
 SELECT 'TN - Level 4', 
     CASE WHEN (program_modifier_id = 'E1AA7A36-0500-4BAE-A0AA-D9E0BC91A6F3' )
        THEN COUNT(DISTINCT people_id) END AS 'Total Ct' 
 from cte 
 group by program_modifier_id
    UNION

 SELECT 'TN - Level 3 CE - RTC', 
     CASE WHEN (program_modifier_id = '213D080F-E340-44B6-AC8C-4233D1193602' 
                      AND license_number like '%-RTC-%')
        THEN COUNT(DISTINCT people_id) END AS 'Total Ct' 
 from cte 
 group by program_modifier_id, license_number 

输出是:

TN - Level 3 CE - RTC   49
TN - Level 3 CE - RTC   38
TN - Level 4    56
TN - Level 3 CE - RTC   7
TN - Level 3 CE - RTC   24
TN - Level 3 CE - RTC   NULL
TN - Level 4    NULL

但我希望输出看起来像:

TN - Level 3 CE - RTC   118
TN - Level 4    56

所以问题是双重的。首先,其中一些计数在 WHERE 子句中包含多个条件。例如,program_modifier_id 与 program_modifier_id 和 license_number。第二个问题是,使用上面的 TSQL 代码,我必须同时按 program_modifier_id 和 license_number 分组,以便在 WHERE 子句中包含 license_number。但我不想对 license_Number 的计数进行细分。我只想要总数,不管 license_number 是多少。如何修改此 TSQL 查询以实现所需的计数?

4

2 回答 2

1
;with cte as 
    (select 
       program_modifier_id, program_modifier, people_id, group_profile_id, 
       current_status, license_number, is_managing_office,
       program_info, program_name, program_code, group_profile_type_id
     From
    #enrollments en with (nolock) )

 Select Description,SUM([Total Ct]) as [Total Ct] from
 (   
 SELECT 'TN - Level 4' as Description, 
     CASE WHEN (program_modifier_id = 'E1AA7A36-0500-4BAE-A0AA-D9E0BC91A6F3' )
        THEN COUNT(DISTINCT people_id) END AS 'Total Ct' 
 from cte 
 group by program_modifier_id
 ) a Group by Description

 UNION

 Select Description,SUM([Total Ct]) from
 (  
 SELECT 'TN - Level 3 CE - RTC'as Description, 
     CASE WHEN (program_modifier_id = '213D080F-E340-44B6-AC8C-4233D1193602' 
                      AND license_number like '%-RTC-%')
        THEN COUNT(DISTINCT people_id) END AS 'Total Ct' 
 from cte 
 group by program_modifier_id, license_number 
 ) a Group by Description
于 2013-05-09T15:17:41.940 回答
0

谢谢。原来是超级简单!这是我的解决方案:

;with cte as (
select 
*
From
    #enrollments en with (nolock)
)

SELECT 'TN - Level 4 TOTAL', count(distinct people_id) from #enrollments where program_modifier_id = 'E1AA7A36-0500-4BAE-A0AA-D9E0BC91A6F3' AND group_profile_id in ('3EC657F7-655F-43FB-8424-2A8914E7C725','9D1628F0-CF85-464C-815A-8660AF60978F','D7C59915-067A-4A03-8B8C-23783568C8D3') AND (@Status is NULL OR current_status = @Status)
UNION
SELECT 'TN - Level 4 IH', count(distinct people_id) from #enrollments where program_modifier_id = 'E1AA7A36-0500-4BAE-A0AA-D9E0BC91A6F3' AND group_profile_id = 'D7C59915-067A-4A03-8B8C-23783568C8D3' AND (@Status is NULL OR current_status = @Status)
UNION
SELECT 'TN - Level 4 CIRT', count(distinct people_id) from #enrollments where program_modifier_id = 'E1AA7A36-0500-4BAE-A0AA-D9E0BC91A6F3' AND group_profile_id = '3EC657F7-655F-43FB-8424-2A8914E7C725' AND (@Status is NULL OR current_status = @Status)
于 2013-05-09T15:46:42.293 回答