总计数大于SQL Server中的某个值

时间:2016-10-26 20:06:56

标签: sql-server aggregate-functions

我有一个按邮政编码对人进行分组的查询。我希望能够将少于10人的邮政编码计数总计为1组。

以下是按邮政编码对人员进行分组的查询:

SELECT 
    CASE 
        WHEN PatInfo.Zip IS NULL THEN 'Unknown Residence'
        ELSE PatInfo.Zip 
    END AS ZipCode,  
    COUNT(PatInfo.PatientProfileID) AS TotalPatient
FROM
    Encounter E 
JOIN 
    EncounterType ET ON E.EncounterTypeID = ET.EncounterTypeID        
JOIN 
    PatientInfo PatInfo ON PatInfo.PatientProfileID = E.PatientProfileID           
WHERE 
    YEAR(Visit) = 2016 
    AND ET.Description IN ('Office Visit', 'Outpatient Surgery', 'Emergency') 
    AND E.EncounterCounted = 1 
GROUP BY
    PatInfo.Zip

我希望能够将10个或更少的计数相加并将它们分成1个分组。像这样:

SELECT 
    'Other Zip Codes' AS ZipCode,  
    SUM(COUNT(PatInfo.PatientProfileID)) AS TotalPatient
FROM 
    Encounter E 
JOIN 
    EncounterType ET ON E.EncounterTypeID = ET.EncounterTypeID        
JOIN 
    PatientInfo PatInfo ON PatInfo.PatientProfileID = E.PatientProfileID           
WHERE 
    YEAR(Visit) = 2016 
    AND ET.Description IN ('Office Visit', 'Outpatient Surgery', 'Emergency')
    AND E.EncounterCounted = 1 
GROUP BY
    PatInfo.Zip
HAVING 
    COUNT(PatInfo.PatientProfileID) <= 10

但是我收到了一个错误:

  

无法对包含的表达式执行聚合函数   聚合

2 个答案:

答案 0 :(得分:0)

我明白了。刚刚创建了一个子查询并将其求和,然后将两个结果联合起来:

SELECT CASE WHEN PatInfo.Zip IS NULL THEN 'Unknown Residence' ELSE PatInfo.Zip END AS ZipCode,  
       COUNT(PatInfo.PatientProfileID) AS TotalPatients
FROM Encounter E JOIN EncounterType ET ON E.EncounterTypeID = ET.EncounterTypeID        
        JOIN PatientInfo PatInfo ON PatInfo.PatientProfileID = E.PatientProfileID           
WHERE YEAR(Visit) = 2016 AND ET.Description IN ('Office Visit', 'Outpatient Surgery', 'Emergency')
        AND E.EncounterCounted = 1 
GROUP BY PatInfo.Zip
HAVING COUNT(PatInfo.PatientProfileID) > 10
UNION ALL
SELECT 'Other Zip Codes' AS ZipCode, SUM(TotalPatient) AS TotalPatients
FROM
(
SELECT 'Other Zip Codes' AS ZipCode,  
       COUNT(PatInfo.PatientProfileID) AS TotalPatient
FROM Encounter E JOIN EncounterType ET ON E.EncounterTypeID = ET.EncounterTypeID        
        JOIN PatientInfo PatInfo ON PatInfo.PatientProfileID = E.PatientProfileID           
WHERE YEAR(Visit) = 2016 AND ET.Description IN ('Office Visit', 'Outpatient Surgery', 'Emergency')
        AND E.EncounterCounted = 1 
GROUP BY PatInfo.Zip
HAVING COUNT(PatInfo.PatientProfileID) <= 10
) AS OtherZips

答案 1 :(得分:0)

试试这个,

declare @year varchar(20)='2016'
set @year=@year +'-01-01'
--select @year
select DATEADD(month,datediff(month,0, DATEADD(month,-month(@year)+1, @year)),0)

;With CTE as
(
SELECT 
    CASE 
        WHEN PatInfo.Zip IS NULL THEN 'Unknown Residence'
        ELSE PatInfo.Zip 
    END AS ZipCode,  
    row_number()over(partition by PatInfo.Zip order by PatInfo.Zip) AS ZipGroup
FROM
    Encounter E 
JOIN 
    EncounterType ET ON E.EncounterTypeID = ET.EncounterTypeID        
JOIN 
    PatientInfo PatInfo ON PatInfo.PatientProfileID = E.PatientProfileID           
WHERE 
    Visit >= @year
    AND ET.Description IN ('Office Visit', 'Outpatient Surgery', 'Emergency') 
    AND E.EncounterCounted = 1 
)

select ZipCode,ZipGroup from CTE
where ZipGroup<10
union all
select ZipCode,ZipGroup from CTE
where ZipGroup>=10