简体   繁体   中英

sql get total of value across multiple columns

I have a sql server 2012 db with a table with 10 columns

name test1, test2, test3, test4,....
 bob  yes    no    null    yes
 john  no    yes    yes    null

I want to get a total of all results from the 'test' fields so i want my results to be

yes = 4
no = 2
null = 2

I have tried using cte, sum, case when but I cant get the results i'm looking for. below is a sample of my sql if anyone could tell me how to go about getting the results i'm looking for.

       with cte as 
  (
SELECT
test1,
sum (case when test1 = 'yes' then 1 else 0 end) as yes,
sum (case when test1= 'no' then 1 else 0 end) as no,
sum (case when test1 is null then 1 else 0 end) as notset
from names
group by Inspection1Type)
  select 
   sum (yes) as 'yes',
    sum(no) as 'no'
     sum(notset) as 'Not Set'
  from cte;

it works for the first column but not for the remaining ones as i'm looking for same value it complains about my aliases being the same

Try this cut&paste approach:

SELECT
    sum (case when test1 = 'yes' then 1 else 0 end
        +case when test2 = 'yes' then 1 else 0 end
        +case when test3 = 'yes' then 1 else 0 end
        ...
        +case when test10 = 'yes' then 1 else 0 end) as yes,
    sum (case when test1 = 'no' then 1 else 0 end
        +case when test2 = 'no' then 1 else 0 end
        +case when test3 = 'no' then 1 else 0 end
        ...
        +case when test10 = 'no' then 1 else 0 end) as no,
    sum (case when test1 is null then 1 else 0 end
        +case when test2 is null then 1 else 0 end
        +case when test3 is null then 1 else 0 end
        ...
        +case when test10 is null then 1 else 0 end) as notset
from names

I like handling this with apply . If you want one row per name :

select n.*, v.*
from names n cross apply
     (select sum(case when v.test = 'yes' then 1 else 0 end) as yes,
             sum(case when v.test = 'no' then 1 else 0 end) as no,
             sum(case when v.test is null then 1 else 0 end) as nulls             
      from (values (n.test1), (n.test2), . . . (n.test10)) v(test)
     ) v;

If you want one row for all the data:

select sum(case when v.test = 'yes' then 1 else 0 end) as yes,
       sum(case when v.test = 'no' then 1 else 0 end) as no,
       sum(case when v.test is null then 1 else 0 end) as nulls 
from names n cross apply
     (values (n.test1), (n.test2), . . . (n.test10)) v(test);

Having repeating columns in a table is usually a sign of an issue with the data model. Normally, you would want one row per name / test , and that would simplify queries on this data.

Try this:

WITH ABC
as
(
SELECT 'bob' as name, 'yes' as test1, 'no' as test2, null as test3, 'yes' as test4
UNION ALL
SELECT 'john', 'no','yes','yes',null
)
SELECT SUM(CASE WHEN A.result = 'yes' then 1 else 0 end) as [Yes], 
       SUM(CASE WHEN A.result = 'no' then 1 else 0 end) as [No],
       SUM(CASE WHEN A.result is null then 1 else 0 end) as [Null]
FROM 
(
    SELECT  name,result
    FROM ABC
    CROSS APPLY(VALUES(test1),(test2),(test3),(test4))  --may add up to test10
    COLUMNNAMES(result)
) as A

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