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
找到两个连续时间戳的时间戳的差异,以及特定参数的每个单独仪表的值的差异。
预期输出:适用于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和3 $ SGP抛出参数A的值。 在17:58 1 $ SGP,2 $ SGP和3 $ SGP抛出参数A的值。 休息要么所有的米都不在一起作为时间戳,如18:05 时间戳中可以忽略秒。 所以,
01:00 1 $ SGP 415.7806091308594-0 01:00 2 $ SGP 393.191162109375-290 01:00 3 $ SGP 3484841472-3061691904
在MS SQL中提出查询时遇到问题。我也不熟悉使用date_format()忽略mysql中的第二个转换方法。
答案 0 :(得分:0)
这是示例表:
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);
以下是查询:
;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;