在SQL查询中包含TOTAL行

时间:2017-09-15 19:54:55

标签: sql sql-server

我有一个非常大的查询,用于从数据库中获取数据。我想知道我是否有可能在所有数据的末尾添加一行,除了第一列之外,还需要所有列的总和。

SELECT
    t2.ProviderName AS REQUESTOR,
    COUNT(e.clientid) AS '# OF CHECKS',
    (SUM(CASE WHEN (e.[Date] <= '6/1/2017' OR e.[Date] BETWEEN '6/1/2017' AND 
'9/1/2017') AND CL.EligibilityStatus = 20 
                 THEN 1 
                 ELSE 0 
         END)) AS '# ELIGIBLE',
    (SUM(CASE WHEN e.[Date]> '9/1/2017' OR EligibilityStatus = 21 
                 THEN 1 
                 ELSE 0 
         END)) AS '# NOTELIGIBLE',
    (SUM(CASE WHEN e.MakeReferral = 110 
                 THEN 1 ELSE 0 
         END)) as '# REFERRED',
    (SUM(CASE WHEN e.makereferral = 111 
                 THEN 1 ELSE 0 
         END)) AS '# NOT REFERRED',
    '' as 'REASON:',
    (SUM(CASE WHEN e.Reason = 60 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'Not on eligibility List',
    (SUM(CASE WHEN e.reason = 61 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'Already Enrolled',
    (SUM(CASE WHEN e.reason = 62 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'Follow-up Needed',
    (SUM(CASE WHEN e.reason = 63 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'Medicaid Issue',
    (SUM(CASE WHEN e.reason = 64 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'QMB',
    (SUM(CASE WHEN e.reason = 65 AND e.MakeReferral = 111 
                 THEN 1 ELSE 0 
         END)) AS 'Other'  
FROM
    tblBHH_ClientEligibility e  
INNER JOIN
    (SELECT
         providerID, providerName 
     FROM
         tblBHH_Providers  
     UNION ALL  
     SELECT 
         id, label 
     FROM
         tblBHH_ReferenceData 
     WHERE
         fldname = 'requestor') t2 ON e.Requestor = t2.ProviderID  
INNER JOIN 
    tblBHH_Clients CL ON e.clientid = CL.ClientID  
WHERE
    e.[date] BETWEEN '6/1/2017' AND '9/1/2017' 
GROUP BY 
    ProviderName  
ORDER BY 
    ProviderName 

因此,此查询产生的数据如下所示:

enter image description here

所以没什么好疯狂的,除了第一列REQUESTOR,它是所有数字,总是> 0或0,没有NULL。

现在我想在REQUESTOR列中添加一个TOTAL行,然后基本上总计所有剩余的列。是这样的吗?

2 个答案:

答案 0 :(得分:2)

使用grouping sets

group by grouping sets ( (ProviderName), () ) 

答案 1 :(得分:0)

自从我对Gordon的答案的评论变得有点冗长以来,我想我会用一些样本数据总结其中一些。

;with data (ProviderName, Value) as
(
    select 'AAA', 1 union all
    select 'AAA', 3.14 union all
    select 'BBB', 987
)
select
    ProviderName = case when grouping(ProviderName) = 0 then ProviderName
                        else 'Total'
                   end,
    Value = sum(Value)
from data
group by grouping sets
(
    (ProviderName),
    ()
)

还有其他一些方法可以做到这一点,但我认为grouping sets是最清晰的,并且最容易转移到其他情况。那就是说,这是你可以做到的另一种方式。由于总行将具有null ProviderName,因此您只需执行ISNULL。我建议您反对这一点的原因是,如果您的group by中有多列,则无论如何都必须使用grouping()函数,因此这是一个更好的习惯。

with rollup选项就是这样做的;累计额外的总排。我似乎记得从with rollup不理想的地方读书或听到,虽然TBH我不记得为什么。

;with data (ProviderName, Value) as
(
    select cast('AAA' as varchar(30)), 1 union all
    select 'AAA', 3.14 union all
    select 'BBB', 987
)
select
    ProviderName = isnull(ProviderName, 'Total'),
    Value = sum(Value)
from data
group by ProviderName
with rollup