使用bit类型列来区分组?

时间:2014-11-12 15:32:49

标签: sql sql-server tsql

我的数据如下所示:

Cell    Date       Hr  Min   Value  Kpi     Exists
CELL1   20141112   8   45    1      KPI1    NULL
CELL1   20141112   8   45    2      KPI1    NULL
CELL1   20141112   8   45    7      KPI1    NULL
CELL1   20141112   8   45    7      KPI1    NULL
CELL2   20141112   8   0     5      KPI2    1
CELL2   20141112   8   15    2      KPI2    1
CELL2   20141112   8   30    9      KPI2    1
CELL2   20141112   8   45    7      KPI2    1
CELL3   20141112   8   0     1      KPI3    1
CELL3   20141112   8   15    3      KPI3    1
CELL3   20141112   8   30    4      KPI3    NULL
CELL3   20141112   8   45    6      KPI3    1

我想按Cell,Date,HR,KPI进行分组以获得SUM(Value),但我想以某种方式对列[Exists]进行分组,以便在分组中出现“NULL”时,将以某种方式区分具有NULL的那些与没有NULL的那些。换句话说,如果4个季度中的任何一个都有NULL,则显示它。

例如,结果看起来像这样:

CELL3   20141112   8    14   NULL
CELL2   20141112   8    23   1
CELL1   20141112   8    17   NULL

这是我目前用于对数据进行分组的查询,有效:

create table #temp
(
 Cell varchar(10),
 Date int,
 Hr   int,
 Min  int,
 Value int,
 Kpi  varchar(10),
 [Exists] bit
)
;
insert into #temp values ('CELL1' ,  20141112 ,  8 ,  45  ,  1    ,  'KPI1'  ,  NULL)
insert into #temp values ('CELL1' ,  20141112 ,  8 ,  45  ,  2    ,  'KPI1'  ,  NULL)
insert into #temp values ('CELL1' ,  20141112 ,  8 ,  45  ,  7    ,  'KPI1'  ,  NULL)
insert into #temp values ('CELL1' ,  20141112 ,  8 ,  45  ,  7    ,  'KPI1'  ,  NULL)
insert into #temp values ('CELL2' ,  20141112 ,  8 ,  0   ,  5    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL2' ,  20141112 ,  8 ,  15  ,  2    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL2' ,  20141112 ,  8 ,  30  ,  9    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL2' ,  20141112 ,  8 ,  45  ,  7    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL3' ,  20141112 ,  8 ,  0   ,  1    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL3' ,  20141112 ,  8 ,  15  ,  3    ,  'KPI1'  ,  1   )
insert into #temp values ('CELL3' ,  20141112 ,  8 ,  30  ,  4    ,  'KPI1'  ,  NULL)
insert into #temp values ('CELL3' ,  20141112 ,  8 ,  45  ,  6    ,  'KPI1'  ,  1   )


SELECT             
Cell
, Date            
, Hr            
,sum(KPI1) as 'KPI1'            
FROM #temp
PIVOT (SUM(Value)             
FOR kpi in             
(KPI1) ) AS pvt               
GROUP BY Cell, 
Date,             
hr         

1 个答案:

答案 0 :(得分:2)

试试这个:case when min(coalesce([Exists],0)) = 0 then null else 1 end as [Exists]

完整查询:

SELECT             
    Cell
   ,Date            
   ,Hr            
   ,sum(KPI1) as 'KPI1'
   ,case when min(coalesce([Exists],0)) = 0 then null else 1 end as [Exists]
FROM #temp
PIVOT (SUM(Value) FOR kpi in (KPI1)) AS pvt               
GROUP BY Cell, Date, hr         

使用您的样本数据输出为:

Cell       Date        Hr          KPI1        Exists
---------- ----------- ----------- ----------- -----------
CELL1      20141112    8           17          NULL
CELL2      20141112    8           23          1
CELL3      20141112    8           14          NULL