0
    时间戳计名称参数名称值
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

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 中的查询时面临问题。我也不熟悉用于忽略在 mysql 中可以处理 date_format() 的第二个转换方法。

4

1 回答 1

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;
于 2014-04-01T12:50:55.137 回答