简体   繁体   中英

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.

Now I'd like to add a TOTAL row in the REQUESTOR column, and then basically take a total of all the remaining columns. 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. 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 . 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.

The with rollup option does just that; 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 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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