基于特定列的不同列的SQL计数

时间:2017-07-17 18:57:03

标签: sql sql-server tsql

我有一张这样的表:

      Insert into Actual_FTE values
     ('Anu ','Pgm1',  '0.2',  '0',  '0.5', ' 0.4', ' 0.6',  '0.7','0.8',  '0.4','0.3', ' 0.4','0.3',  '0.2','2017',  'Actual'),                             
     ('Barry ','Pgm1',  '0.4',  '0.6',  '0.3', ' 0.1', ' 0',  '0.8','0.8',  '0.7','0.8', ' 0.9','0.3',  '0.3','2017',  'Actual'),                               

     ('Anu ','Pgm1',  '1',  '1',  '0.8', ' 0.8', ' 0.9',  '0.9','0.9',  '1','1', ' 1','0.7',  '0.7','2017',  'Forecast 1'),                             
    ('Barry ','Pgm1',  '0.8',  '0.7',  '0.7', ' 0.9', ' 0.7',  '0.4','0.5',  '0.6','0.8', ' 0.8','0.8',  '0.8','2017',  'Forecast 1'),                              

     ('Betsy ','Pgm1',  '0.8',  '0.6',  '0.4', ' 0.3', ' 0.3',  '0.3','0.2',  '0.1','0.3', ' 0.4','0.6',  '0.5','2017',  'Forecast 1'),                             

     ('Cory ','Pgm1',  '0.8',  '0.6',  '0.7', ' 0.4', ' 0.3',  '0.4','0.3',  '0.5','0.2', ' 0.3','0.2',  '0.1','2017',  'Forecast 1'),                              

   ('Anu ','Pgm1',  '0.5',  '0.3',  '0.4', ' 0.5', ' 0.4',  '0.4','0.8',  '0.2','0.8', ' 0.6','0.8',  '0.7','2017',  'Forecast 2'),                             

     ('Betsy ','Pgm1',  '0.8',  '0.6',  '0.7', ' 0.8', ' 0.9',  '0.4','0.3',  '0.5','0.4', ' 0.5','0.4',  '0.2','2017',  'Forecast 2'),                             

     ('Cory ','Pgm1',  '0.3',  '0.5',  '0.6', ' 0.5', ' 0.5',  '0.6','0.8',  '0.9','1', ' 1','1',  '0.9','2017',  'Forecast 2'),                                
     ('Barry ','Pgm1',  '0.2',  '0',  '0.5', ' 0.4', ' 0.6',  '0.7','0.8',  '0.4','0.3', ' 0.4','0.3',  '0.2','2017',  'Forecast 3'),                               

     ('Betsy ','Pgm1',  '0.4',  '0.6',  '0.3', ' 0.1', ' 0',  '0.8','0.8',  '0.7','0.8', ' 0.9','0.3',  '0.3','2017',  'Forecast 3')        

样本数据是

 Program Jan  Feb Mar Apri May Jun July Aug Sep Oct Nov Dec Year Scenario
 Pgm1     2    1    2   2   1   2   2    2   2   2   2   2   2017  Actual
 Pgm1     4    4    4   4   4   4   4    4   4   4   4   4   2017  Forecast1
 Pgm1     3    3    3   3   3   3   3    3   3   3   3   3   2017  Forecast2
 Pgm1     2    1    2   2   2   1   2    2   2   2   2   2   2017  Forecast3

我们的想法是在整个日历年中找出有多少员工正在为各种情况工作 - 实际和一些预测数据。我希望获得每个场景的员工数量。如果任何mnonth的值为0,则表示员工正在该月工作且不应计算在内。例如,输出应该是这样的:

for _, device := range devices {
        currentDeviceLabel := device.GetVirtualDevice().DeviceInfo.GetDescription().Label
        if strings.Contains(strings.ToLower(currentDeviceLabel), "hard disk"){
        disks = append(disks, device)
    }    
return disks

请让我知道怎么做。

谢谢, HEMA

2 个答案:

答案 0 :(得分:2)

你可以利用count忽略null的方式获得一个明显的enames计数,忽略字段值为'0'的行。

SELECT
    ProgramName
    , COUNT (DISTINCT Case January When '0' then Null else EName end) as January
    , COUNT (DISTINCT Case February When '0' then Null else EName end) as February
    , COUNT (DISTINCT Case March When '0' then Null else EName end) as March
    , COUNT (DISTINCT Case April When '0' then Null else EName end) as April
    , COUNT (DISTINCT Case May When '0' then Null else EName end) as May
    , COUNT (DISTINCT Case June When '0' then Null else EName end) as June
    , COUNT (DISTINCT Case July When '0' then Null else EName end) as July
    , COUNT (DISTINCT Case August When '0' then Null else EName end) as August
    , COUNT (DISTINCT Case September When '0' then Null else EName end) as September
    , COUNT (DISTINCT Case October When '0' then Null else EName end) as October
    , COUNT (DISTINCT Case November When '0' then Null else EName end) as November
    , COUNT (DISTINCT Case December When '0' then Null else EName end) as December
    , [Year]
    , Scenario
From Actual_FTE
Group by
    ProgramName
    , [Year]
    , Scenario

告诉你:

ProgramName    January     February    March       April       May         June        July        August      September   October     November    December    Year   Scenario
-------------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------- ------ ----------
Pgm1           2           1           2           2           2           2           2           2           2           2           2           2           2017   Actual
Pgm1           4           4           4           4           4           4           4           4           4           4           4           4           2017   Forecast 1
Pgm1           3           3           3           3           3           3           3           3           3           3           3           3           2017   Forecast 2
Pgm1           2           1           2           2           2           2           2           2           2           2           2           2           2017   Forecast 3

答案 1 :(得分:1)

@Greenspak稍微改进版本,以涵盖使用前导空格错误输入数字的情况。就像你的样本数据一样。见五月。

SELECT
    ProgramName,
    COUNT (DISTINCT Case convert(float,January) When 0 then Null else EName end) as January,
    COUNT (DISTINCT Case convert(float,February) When 0 then Null else EName end) as February,
    COUNT (DISTINCT Case convert(float,March) When 0 then Null else EName end) as March,
    COUNT (DISTINCT Case convert(float,April) When 0 then Null else EName end) as April,
    COUNT (DISTINCT Case convert(float,May) When 0 then Null else EName end) as May,
    COUNT (DISTINCT Case convert(float,June) When 0 then Null else EName end) as June,
    COUNT (DISTINCT Case convert(float,July) When 0 then Null else EName end) as July,
    COUNT (DISTINCT Case convert(float,August) When 0 then Null else EName end) as August,
    COUNT (DISTINCT Case convert(float,September) When 0 then Null else EName end) as September,
    COUNT (DISTINCT Case convert(float,October) When 0 then Null else EName end) as October,
    COUNT (DISTINCT Case convert(float,November) When 0 then Null else EName end) as November,
    COUNT (DISTINCT Case convert(float,December) When 0 then Null else EName end) as December,
    [Year],
    Scenario
From Actual_FTE
Group by
    ProgramName,
    [Year],
    Scenario