0
    time stamp        meter name  parameter name        value
2014-02-18 18:58:00      1$SGP          A          415.7806091308594
2014-02-18 18:58:00      1$SGP           B          240.3373565673828
2014-02-18 18:58:01      2$SGP           A          393.191162109375
2014-02-18 18:58:02      2$SGP           B           50.10090637207031
2014-02-18 18:58:00      3$SGP           A          3484841472
2014-02-18 18:05:01      1$SGP           A          0
2014-02-18 17:58:01      1$SGP           A          0
2014-02-18 17:58:01      2$SGP           A          290
2014-02-18 17:58:01      2$SGP           D          0
2014-02-18 17:58:01      3$SGP           A          3061691904
2014-02-18 17:57:01      3$SGP           A          0
2014-02-18 17:57:02      3$SGP           B          0

find the difference in time stamps of two consecutive time stamp and the difference in their value for each individual meter for a particular parameter.

Expected Output: for 18:58

01:00 1$SGP 415.7806091308594-0 01:00 2$SGP 393.191162109375-290 01:00 3$SGP 3484841472-3061691904

At, 18:58 1$SGP,2$SGP and 3$SGP throwing values for parameter A. At 17:58 1$SGP,2$SGP and 3$SGP throwing values for parameter A. Rest either all meters are not present together for a time stamp as in 18:05 Seconds can be ignored in the time stamp. So,

01:00 1$SGP 415.7806091308594-0 01:00 2$SGP 393.191162109375-290 01:00 3$SGP 3484841472-3061691904

Facing problem in coming up with the query in MS SQL. I am also not familiar with the convert method for ignoring the second which in mysql can be dealth with date_format().

BAdmin
  • 927
  • 1
  • 11
  • 19
Arun
  • 187
  • 11

1 Answers1

0

Here's the sample table:

create table #t(ts datetime, mn char(5), pn char, val decimal (25,15));

insert #t values 
('2014-02-18 18:58:00',      '1$SGP',           'A',          415.7806091308594),
('2014-02-18 18:58:00',      '1$SGP',           'B',          240.3373565673828),
('2014-02-18 18:58:01',      '2$SGP',           'A',          393.191162109375),
('2014-02-18 18:58:02',      '2$SGP',           'B',           50.10090637207031),
('2014-02-18 18:58:00',      '3$SGP',           'A',          3484841472),
('2014-02-18 18:05:01',      '1$SGP',           'A',          0),
('2014-02-18 17:58:01',      '1$SGP',           'A',          0),
('2014-02-18 17:58:01',      '2$SGP',           'A',          290),
('2014-02-18 17:58:01',      '2$SGP',           'D',          0),
('2014-02-18 17:58:01',      '3$SGP',           'A',          3061691904),
('2014-02-18 17:57:01',      '3$SGP',           'A',          0),
('2014-02-18 17:57:02',      '3$SGP',           'B',          0);

And here's the query:

;with t as (
    select cast(ts as smalldatetime) as ts, mn, pn, val from #t
)
,
tt as (
    select ts from t
    group by ts
    having count(*) >= 3
),
ttt as (
    select *, row_number() over(partition by mn, pn order by ts desc) as rn
    from t where t.ts in (select tt.ts from tt)
)
select t1.ts, t1.mn, t1.val, t2.val
from ttt t1
inner join ttt t2 on t1.mn = t2.mn and t1.pn = t2.pn and t1.rn = 1 and t2.rn = 2;
dean
  • 9,960
  • 2
  • 25
  • 26
  • If this answer was useful to you, please accept / upvote so the others can benefit from it. – dean Apr 03 '14 at 18:30