我有一张像
这样的表格 Id_indicator Value trend Date_data
1 0 0 2011-08-18 09:16:15
1 2 1 2011-08-18 10:16:15
1 1 -1 2011-08-18 11:16:15
1 2 1 2011-08-18 12:16:15
2 21 0 2011-08-18 13:16:15
2 21 0 2011-08-18 14:16:15
2 21 0 2011-08-18 15:16:15
3 3 0 2011-08-18 16:16:15
3 4 1 2011-08-18 17:16:15
3 4 0 2011-08-18 18:16:15
4 4 0 2011-08-18 19:16:15
我需要找出基于id_indicator的先前值之间的区别,并在右侧添加一列并输入该值。
的例子 Id_indicator Value trend Date_data Difference
1 0 0 2011-08-18 09:16:15 0
1 2 1 2011-08-18 10:16:15 2
1 1 -1 2011-08-18 11:16:15 -1
1 2 1 2011-08-18 12:16:15 1
2 21 0 2011-08-18 13:16:15 0
2 21 0 2011-08-18 14:16:15 0
2 21 0 2011-08-18 15:16:15 0
3 3 0 2011-08-18 16:16:15 0
3 4 1 2011-08-18 17:16:15 1
3 4 0 2011-08-18 18:16:15 0
4 4 0 2011-08-18 19:16:15 0
由于
答案 0 :(得分:0)
您可以先创建一个查找,为您提供每个ID和日期的上一个时间戳,然后加入此查找。
像这样(未经测试):
SELECT * -- t1.value - t2.vaue...
FROM
(SELECT t1.Id_indicator,
t1.Date_data,
max(t2.Date_date) AS prev_date_data
FROM TABLE t1
LEFT JOIN TABLE t2 ON t1.Id_indicator = t2.Id_indicator
AND t2.Date_data < t1.Date_data
GROUP BY 1, 2) AS lookup
LEFT JOIN TABLE t1 ON t1.Id_indicator = lookup.Id_indicator
AND t1.Date_data = lookup.Date_data
LEFT JOIN TABLE t2 ON t2.Id_indicator = lookup.Id_indicator
AND t2.Date_data = lookup.prev_date_data