按列名称计算的总计数

时间:2015-02-12 21:28:23

标签: sql group-by sum

我正在尝试将数据放入数据集中。我已尝试在列名称和选择组中使用group by。我无法弄清楚正确的语法。

期望的结果:

 ColumnName |   Value
   a              2
   b              9
   c              12 
   d              15
   e              19
   f              14
   g              11

这是它目前返回的内容

Column Name   a    b      c      d     e     f     g
Value         2    9      12     15    19    14    11

我的查询

SELECT
SUM(CASE 
WHEN Note like '%is correct%' then 1 else 0 end) AS a,

SUM(CASE 
WHEN Note like '%is incorrect%' then 1 else 0 end) AS b,

SUM(CASE 
WHEN Note like '%not be reached%' then 1 else 0 end) AS         c,

SUM(CASE 
WHEN Note like '%not attempted%' then 1 else 0 end) AS d,

SUM(CASE 
WHEN Note like '%on site by EVA%' then 1 else 0 end) AS e,

SUM(CASE 
WHEN Note like '%Initial followup call requested%' then 1 else 0 end) AS f,

SUM(CASE 
WHEN Note like '%assigned by IVR%' then 1 else 0 end) AS VendorAssignedByIVR,
SUM(CASE 
WHEN Note like '%Vendor refused%' then 1 else 0 end) AS VendorRefused,

SUM(CASE 
WHEN Note like '%is too soon for multiple%' then 1 else 0 end) AS g,

SUM(CASE 
WHEN Note like '%is too soon for multiple%' then 1 else 0 end) AS     h

from fna..tblEventNotes n

1 个答案:

答案 0 :(得分:0)

你可以用工会来做。对每一列进行单独选择并将它们组合在一个联合中。但如果它在GUI中呈现它更容易使用任何形式的模板(取决于组件)。