sql server - Adding column which groups data by time -


i've got table shows: power, time, diff-days, diff-hours, diff-minutes diff columns use datediff , lag calculate difference in times between rows.

(powerkw) (time)               (diff-days) (diff-hours) (diff-minutes)     31011.39  2014-01-01 00:30:00  null        null         null    31838.74  2014-01-01 00:40:00  0           0            -10    32356.35  2014-01-01 00:50:00  0           0            -10    32358.82  2014-01-01 01:00:00  0           -1           -10    32414.15  2014-01-01 01:10:00  0           0            -10    32413.81  2014-01-01 01:20:00  0           0            -10    32412.35  2014-01-01 01:30:00  0           0            -10    32416.23  2014-01-01 01:40:00  0           0            -10    32014.94  2014-01-01 01:50:00  0           0            -10    31184.45  2014-01-01 03:40:00  0           -2           -110    32403.38  2014-01-01 03:50:00  0           0            -10    32415.07  2014-01-01 04:00:00  0           -1           -10    32388.04  2014-01-01 04:10:00  0           0            -10    32320.70  2014-01-01 04:20:00  0           0            -10    32297.44  2014-01-01 04:30:00  0           0            -10 

what want 6th column groups these rows events happening consecutively, i.e 9 happening every 1 minutes 1 after other have 1 in 6th column there 2 hour difference , 6 rows happening after each other have 2 in 6th column, possible?
i.e.

(powerkw) (time)               (diff-days) (diff-hours) (diff-minutes) (group)    31011.39  2014-01-01 00:30:00  null        null         null           1    31838.74  2014-01-01 00:40:00  0           0            -10            1               32356.35  2014-01-01 00:50:00  0           0            -10            1               32358.82  2014-01-01 01:00:00  0           -1           -10            1    32414.15  2014-01-01 01:10:00  0           0            -10            1   32413.81  2014-01-01 01:20:00  0           0            -10            1    32412.35  2014-01-01 01:30:00  0           0            -10            1    32416.23  2014-01-01 01:40:00  0           0            -10            1    32014.94  2014-01-01 01:50:00  0           0            -10            1    31184.45  2014-01-01 03:40:00  0           -2           -110           2    32403.38  2014-01-01 03:50:00  0           0            -10            2    32415.07  2014-01-01 04:00:00  0           -1           -10            2    32388.04  2014-01-01 04:10:00  0           0            -10            2    32320.70  2014-01-01 04:20:00  0           0            -10            2    32297.44  2014-01-01 04:30:00  0           0            -10            2 

if definition of consecutive based on "diff minutes" being greater value (or less than, given these negative), can use cumulative sum:

with q (<your query here>) select q.*,        sum(case when diff_minutes < -50 1 else 0 end) on (order time) grp q; 

if want start @ "1", add 1 value.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -