SQL将行转置为没有聚合的列

时间:2017-01-04 11:01:52

标签: sql-server gaps-and-islands

我知道有很多答案,但我没有使用。 我的桌子看起来像

Exception.TargetSite

我们在给定时间内存储仪表开关状态。每次关闭状态后都有一个开启状态。我们想像这样转置表

    MeterNo |       [power off]       |          [power on]
____________________________________________________________________
  x0039938  | 2016-10-08 14:14:37.610 | 2016-10-08 14:17:15.047
____________________________________________________________________
  x0039938  | 2016-10-08 14:20:50.257 | NULL
____________________________________________________________________
  x0039938  | 2016-10-08 14:23:07.610 | 2016-10-08 14:23:17.920
____________________________________________________________________
  x0039940  | 2016-10-08 15:45:38.250 | 2016-10-08 15:52:40.080
____________________________________________________________________

等等

我试过了这个查询

    SELECT *
  FROM
(
  SELECT MeterNo, Status, CreatedOn
    FROM [BreakdownShutdowDetails]
) s
PIVOT
(
  MAX(CreatedOn) FOR Status IN ( [power off], [power on])
) p

只给出了每个meterno的结果

enter image description here

获取每个仪表的On on On状态之间的持续时间的基本思路。 请建议正确的查询。 感谢。

更新1 样本数据

1   x0039938    power off   2016-10-08 14:14:37.610
2   x0039938    power on    2016-10-08 14:17:15.047
3   x0039938    power off   2016-10-08 14:20:50.257
4   x0039938    power off   2016-10-08 14:23:07.610
5   x0039938    power on    2016-10-08 14:23:17.920
6   x0039940    power off   2016-10-08 15:45:38.250
7   x0039940    power on    2016-10-08 15:52:40.080
8   x0040281    power off   2016-10-08 15:59:26.513
9   x0040281    power on    2016-10-08 16:20:23.323
10  x0039940    power off   2016-10-08 16:26:29.133
19  x0040281    power off   2016-10-08 17:17:48.900
22  x0039937    power off   2016-10-08 17:24:24.617
23  x0039937    power on    2016-10-08 17:24:38.590
24  x0039937    power off   2016-10-08 17:33:31.843
25  x0039937    power on    2016-10-08 17:35:47.470
27  x0039940    power off   2016-10-08 17:37:18.360
28  x0040281    power on    2016-10-08 17:40:08.093
30  x0043637    power off   2016-10-09 14:32:23.130
31  x0039937    power off   2016-10-09 14:32:24.893
32  x0040281    power off   2016-10-09 14:32:27.387
33  x0039940    power off   2016-10-09 14:32:29.407
34  x0040281    power on    2016-10-09 15:01:42.480

1 个答案:

答案 0 :(得分:2)

它通常称为https://jsfiddle.net/fabio_p/7pf4cqrg/问题。尝试这样的事情(未经测试)

SELECT MeterNo,
       [power off] = Min(CASE
                           WHEN status = 'power off' THEN CreatedOn
                         END),
       [power on] = Min(CASE
                           WHEN status = 'power on' THEN CreatedOn
                         END)
FROM   (SELECT *,
                Row_number() OVER (partition BY MeterNo ORDER BY CreatedOn) - 
                Dense_rank() OVER (partition BY MeterNo ORDER BY status)  AS seq_grp
        FROM   Yourtable) t
GROUP  BY MeterNo,
          seq_grp