0

在我的脚本中,我有一个显示 SQL 查询结果的表。但是,我将结果用作摘要,但 SQL 使页面显示表的次数与返回 SQL 中由于 Where 子句而允许的结果(行)一样多。即如果汇总了 3 组人,则表格重复相同的信息 3 次

有没有解决这个问题的常用方法?谢谢!

SELECT 
    Demographics.Name,
    Demographics.NDoc_Number,
    Demographics.PID_alphanumeric,
    Demographics.Company,
    Demographics.Company_Name,
    Demographics.Location,
    Demographics.Location_Name,
    Demographics.Team_CMT, 
    Demographics.Case_Manager,
    Demographics.Case_Manager_UID,
    Demographics.SiteName, 
    CareEpisodes.BGNDATE, 
    CareEpisodes.BRFA, 
    CareEpisodes.ENDDATE, 
    CareEpisodes.ERFA,
    OASIS_Improvement.BGNRPT,
    OASIS_Improvement.ENDRPT,
    ISNULL(SUM(CASE OASIS_Improvement.O_I_@Request.MooNum~
        WHEN 'NA'
        THEN 1
        ELSE 0
    END),0) AS numberImproveNA,
    ISNULL(SUM(CASE OASIS_Stabilization.O_S_@Request.MooNum~
        WHEN 'NA'
        THEN 1
        ELSE 0
    END),0) AS numberStabilizeNA,
    ISNULL(SUM(CASE OASIS_Improvement.O_I_@Request.MooNum~
        WHEN 'NA'
        THEN 0
        ELSE 1
    END),0) AS couldShowImprovement,
    ISNULL(SUM(CASE OASIS_Stabilization.O_S_@Request.MooNum~
        WHEN 'NA'
        THEN 0
        ELSE 1
    END),0) AS couldShowStabilization,
    ISNULL(COUNT(Demographics.Name),0) AS patientCount,
    ISNULL(SUM(CASE OASIS_Improvement.O_I_@Request.MooNum~
        WHEN 1
        THEN 1
        ELSE 0
    END),0) AS doShowImprovement,
    ISNULL(SUM(CASE OASIS_Stabilization.O_S_@Request.MooNum~
        WHEN 1
        THEN 1
        ELSE 0
    END),0) AS doShowStabilization

FROM 
    Demographics  
     INNER JOIN OASIS_Improvement  ON 
    Demographics.NDoc_Number = OASIS_Improvement.NDocNumber 
     INNER JOIN OASIS_Stabilization  ON 
    Demographics.NDoc_Number = OASIS_Stabilization.NDocNumber AND
        OASIS_Improvement.BGNRPT=OASIS_Stabilization.BGNRPT
     INNER JOIN CareEpisodes  ON 
    Demographics.NDoc_Number = CareEpisodes.NDocNumber AND
        OASIS_Improvement.BGNRPT=CareEpisodes.BGNRPT
WHERE
    (Demographics.Company IN (@SingleQuote.Request.companyInput~) OR '@Request.companyInput~' = '') AND
    (Demographics.Location IN (@SingleQuote.Request.locationInput~) OR '@Request.locationInput~' = '') AND
    (Demographics.Team_CMT IN (@SingleQuote.Request.teamInput~) OR '@Request.teamInput~' = '') AND
    (Demographics.Case_Manager_UID IN (@SingleQuote.Request.clinicianInput~) OR '@Request.clinicianInput~' = '') AND
    CareEpisodes.ERFA <> 6 AND
    (OASIS_Improvement.O_I_@Request.MooNum~ = 'NA' OR
    OASIS_Improvement.O_I_@Request.MooNum~ = 1 OR
    OASIS_Improvement.O_I_@Request.MooNum~ = 0) AND
    (OASIS_Stabilization.O_S_@Request.MooNum~ = 'NA' OR
    OASIS_Stabilization.O_S_@Request.MooNum~ = 0 OR
    OASIS_Stabilization.O_S_@Request.MooNum~ = 1) AND
    CareEpisodes.BGNDATE >= '@Request.FromDateInput~' AND
    CareEpisodes.ENDDATE <= '@Request.ThruDateInput~'
4

1 回答 1

2

如果您尝试对信息进行分组,则需要考虑使用 GROUP BY 语句。如果您想总结信息,请尝试查看 GROUP BY 但 WITH ROLLUP。ROLLUP 将允许您根据您在 GROUP BY 中选择的值对信息进行分组和汇总。

于 2012-07-06T19:30:39.630 回答