简体   繁体   English

在SQL查询中包含TOTAL行

[英]Including a TOTAL row in a SQL query

I have a pretty big query that I use to get data from the DB. 我有一个很大的查询,我从数据库中获取数据。 I'm wondering if it's possible for me to add a row at the end of all the data, that would take a sum of all the columns with the exception of the first one. 我想知道是否有可能在所有数据的末尾添加一行,这将需要除第一列外的所有列的总和。

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 

So this query yields data that looks like this: 因此,此查询产生的数据如下所示:

在此处输入图片说明

So nothing crazy, with the exception of the first column REQUESTOR, it's all numbers, always going to be >0 or 0, no NULLs. 因此,除了第一列REQUESTOR之外,没有什么疯狂的了,它都是数字,总是> 0或0,没有NULL。

Now I'd like to add a TOTAL row in the REQUESTOR column, and then basically take a total of all the remaining columns. 现在,我想在REQUESTOR列中添加TOTAL行,然后基本上将所有剩余的列合计。 Is something like that doable? 这样可行吗?

使用grouping sets

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

Just since my comment on Gordon's answer was getting a little lengthy, I thought I'd summarize some of this using some sample data. 就在我对戈登答案的评论变得冗长的时候,我想我将使用一些样本数据来总结其中的一些内容。

;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),
    ()
)

There are a couple other ways you could do this as well, but I thing the grouping sets is the clearest, and most transferable to other situations. 您也可以通过其他几种方式来执行此操作,但是我认为grouping sets最清晰,并且最容易转移到其他情况。 That said, Here's another way you could do it. 就是说,这是您可以做的另一种方法。 Since the total row will have a null ProviderName, you could just do an ISNULL . 由于总行的ProviderName为空,因此您可以执行ISNULL The reason I suggest against this is if you ever have more than one column in your group by , you'd have to use the grouping() function anyway, so it's a better habit to get into. 我建议您这样做的原因是,如果您的group by有多个列,则无论如何都必须使用grouping()函数,因此这是一种更好的习惯。

The with rollup option does just that; with rollup选项可以做到这一点。 rolls up an additional total row. 汇总一个额外的总行。 I seem to remember reading or hearing from somewhere that with rollup was not ideal, although TBH I can't remember why. 我似乎记得从某个地方阅读或收听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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM