涉及空值和空白值与填充值的复杂SQL Server数据透视表

时间:2018-06-29 19:56:37

标签: sql-server null pivot grouping blank-line

我需要将“ dep”列分为2个不同的列,一个是“已记录”,另一个是“未记录”。 “未记录”列将包含该日期“ dep”为空白或为空的条目的时间总和。 “已记录”列需要显示当天的时间总和,而“深度”列不能为空或空白。

到目前为止,这就是我拥有的

MATCH (m:Person {gender: 'male'})
WITH COLLECT(m) AS males
MATCH (f:Person {gender: 'female'})
RETURN males, COLLECT(f) AS females

它产生了这个

 SELECT Cast(Start_Time AS DATE) AS Date, dep, 
 sum(time) as "Total Time"
 FROM A6K_Events
 Group By Cast(Start_Time AS DATE), dep

这就是我希望最终的结果

 +------------+--------------+------------+
 | Date       | Dep          | Total Time |
 +------------+--------------+------------+
 | 2018-06-29 | Null         | 3544       |
 +------------+--------------+------------+
 | 2018-06-29 | Other        | 268        |
 +------------+--------------+------------+
 | 2018-06-29 | Training     | 471        |
 +------------+--------------+------------+
 | 2018-06-29 | Change Point | 371        |
 +------------+--------------+------------+
 | 2018-06-28 | Null         | 4519       |
 +------------+--------------+------------+
 | 2018-06-28 | Training     | 1324       |
 +------------+--------------+------------+
 | 2018-06-28 |              | 50         |
 +------------+--------------+------------+

任何建议或帮助将不胜感激。我无法弄清楚如何将空值和空白值旋转和过滤到一列中,而其他值将填充到另一列中。

谢谢。

1 个答案:

答案 0 :(得分:1)

不需要数据透视,我们可以使用CASE语句获取数据

 SELECT start_time as Date, sum(case when dep is not NULL or dep <> '' then time end) as "Recorded", sum(case when dep is NULL or dep = '' then time end) as "Unrecorded"
 FROM A6K_Events
 Group By start_time order by 1 desc