返回两个连续结果之间的“差异”

时间:2013-12-16 19:22:54

标签: mysql sql perl

问题:我可以代替显示Measured值显示值的变化吗?我有perl代码,我希望SQL返回perl代码返回的内容,而不是必须使用代码将其转换为所需的数据。

背景:我有以下SQL表:

CREATE TABLE `Stats` (
 `ID` int(11) NOT NULL AUTO_INCREMENT,
 `MeasuredTime` datetime DEFAULT NULL,
 `Measured` int(11) DEFAULT NULL,
 `What` int(2) DEFAULT NULL,
 PRIMARY KEY (`ID`)
)

和SQL数据:

+-------+---------------------+----------+------+
| ID    | MeasuredTime        | Measured | What |
+-------+---------------------+----------+------+
| 45403 | 2013-12-16 14:01:01 |     4379 |    5 |
| 45398 | 2013-12-16 13:31:01 |     4379 |    5 |
| 45393 | 2013-12-16 13:01:01 |     4379 |    5 |
| 45388 | 2013-12-16 12:31:02 |     4379 |    5 |
| 45383 | 2013-12-16 12:01:01 |     4379 |    5 |
| 45378 | 2013-12-16 11:31:01 |     4378 |    5 |
| 45373 | 2013-12-16 11:01:01 |     4378 |    5 |
| 45368 | 2013-12-16 10:31:01 |     4378 |    5 |
| 45363 | 2013-12-16 10:01:01 |     4378 |    5 |
| 45358 | 2013-12-16 09:31:01 |     4378 |    5 |
| 45353 | 2013-12-16 09:01:02 |     4377 |    5 |
| 45348 | 2013-12-16 08:31:01 |     4377 |    5 |
| 45343 | 2013-12-16 08:01:02 |     4378 |    5 |
| 45338 | 2013-12-16 07:31:01 |     4378 |    5 |
| 45333 | 2013-12-16 07:01:01 |     4378 |    5 |
| 45328 | 2013-12-16 06:31:02 |     4378 |    5 |
| 45323 | 2013-12-16 06:01:01 |     4378 |    5 |
| 45318 | 2013-12-16 05:31:01 |     4378 |    5 |
| 45313 | 2013-12-16 05:01:01 |     4377 |    5 |
| 45308 | 2013-12-16 04:31:02 |     4378 |    5 |
| 45303 | 2013-12-16 04:01:01 |     4377 |    5 |
| 45298 | 2013-12-16 03:31:02 |     4376 |    5 |
| 45293 | 2013-12-16 03:01:01 |     4376 |    5 |
| 45288 | 2013-12-16 02:31:01 |     4376 |    5 |
| 45283 | 2013-12-16 02:01:01 |     4375 |    5 |
| 45278 | 2013-12-16 01:31:01 |     4378 |    5 |
| 45273 | 2013-12-16 01:01:02 |     4378 |    5 |
| 45268 | 2013-12-16 00:31:01 |     4378 |    5 |
| 45263 | 2013-12-16 00:01:01 |     4378 |    5 |
| 45258 | 2013-12-15 23:31:01 |     4378 |    5 |
| 45253 | 2013-12-15 23:01:01 |     4378 |    5 |
| 45248 | 2013-12-15 22:31:01 |     4378 |    5 |
| 45243 | 2013-12-15 22:01:02 |     4378 |    5 |
| 45238 | 2013-12-15 21:31:01 |     4378 |    5 |
| 45233 | 2013-12-15 21:01:01 |     4377 |    5 |
| 45228 | 2013-12-15 20:31:02 |     4377 |    5 |
| 45223 | 2013-12-15 20:01:02 |     4377 |    5 |
| 45218 | 2013-12-15 19:31:02 |     4377 |    5 |
| 45213 | 2013-12-15 19:01:01 |     4377 |    5 |
| 45208 | 2013-12-15 18:31:01 |     4376 |    5 |
| 45203 | 2013-12-15 18:01:02 |     4376 |    5 |
| 45198 | 2013-12-15 17:31:01 |     4377 |    5 |
| 45193 | 2013-12-15 17:01:01 |     4376 |    5 |
| 45188 | 2013-12-15 16:31:01 |     4376 |    5 |
| 45183 | 2013-12-15 16:01:01 |     4375 |    5 |
| 45178 | 2013-12-15 15:31:01 |     4375 |    5 |
| 45173 | 2013-12-15 15:01:01 |     4375 |    5 |
| 45168 | 2013-12-15 14:31:01 |     4375 |    5 |
| 45163 | 2013-12-15 14:01:02 |     4375 |    5 |
| 45158 | 2013-12-15 13:31:01 |     4375 |    5 |
| 45153 | 2013-12-15 13:01:01 |     4375 |    5 |
| 45148 | 2013-12-15 12:31:01 |     4375 |    5 |
| 45143 | 2013-12-15 12:01:02 |     4375 |    5 |
| 45138 | 2013-12-15 11:31:01 |     4375 |    5 |
| 45133 | 2013-12-15 11:01:01 |     4375 |    5 |
| 45128 | 2013-12-15 10:31:01 |     4376 |    5 |
| 45123 | 2013-12-15 10:01:01 |     4376 |    5 |
| 45118 | 2013-12-15 09:31:01 |     4376 |    5 |
| 45113 | 2013-12-15 09:01:01 |     4378 |    5 |
| 45108 | 2013-12-15 08:31:01 |     4381 |    5 |
| 45103 | 2013-12-15 08:01:01 |     4381 |    5 |
| 45098 | 2013-12-15 07:31:01 |     4381 |    5 |
| 45093 | 2013-12-15 07:01:01 |     4383 |    5 |
| 45088 | 2013-12-15 06:31:01 |     4383 |    5 |
| 45083 | 2013-12-15 06:01:01 |     4384 |    5 |
| 45078 | 2013-12-15 05:31:01 |     4384 |    5 |
| 45073 | 2013-12-15 05:01:01 |     4384 |    5 |
| 45068 | 2013-12-15 04:31:01 |     4383 |    5 |
| 45063 | 2013-12-15 04:01:01 |     4383 |    5 |
| 45058 | 2013-12-15 03:31:01 |     4383 |    5 |
| 45053 | 2013-12-15 03:01:01 |     4383 |    5 |
| 45048 | 2013-12-15 02:31:02 |     4383 |    5 |
| 45043 | 2013-12-15 02:01:01 |     4384 |    5 |
| 45038 | 2013-12-15 01:31:01 |     4384 |    5 |
| 45033 | 2013-12-15 01:01:01 |     4385 |    5 |
| 45028 | 2013-12-15 00:31:01 |     4385 |    5 |
| 45023 | 2013-12-15 00:01:01 |     4385 |    5 |
| 45018 | 2013-12-14 23:31:01 |     4385 |    5 |
| 45013 | 2013-12-14 23:01:01 |     4384 |    5 |
| 45008 | 2013-12-14 22:31:01 |     4384 |    5 |
| 45003 | 2013-12-14 22:01:01 |     4384 |    5 |
| 44998 | 2013-12-14 21:31:01 |     4384 |    5 |
| 44993 | 2013-12-14 21:01:02 |     4384 |    5 |
| 44988 | 2013-12-14 20:31:01 |     4384 |    5 |
| 44983 | 2013-12-14 20:01:01 |     4384 |    5 |
| 44978 | 2013-12-14 19:31:01 |     4384 |    5 |
| 44973 | 2013-12-14 19:01:01 |     4384 |    5 |
| 44968 | 2013-12-14 18:31:01 |     4384 |    5 |
| 44963 | 2013-12-14 18:01:02 |     4384 |    5 |
| 44958 | 2013-12-14 17:31:01 |     4384 |    5 |
| 44953 | 2013-12-14 17:01:01 |     4384 |    5 |
| 44948 | 2013-12-14 16:31:01 |     4384 |    5 |
| 44943 | 2013-12-14 16:01:01 |     4384 |    5 |
| 44938 | 2013-12-14 15:31:01 |     4384 |    5 |
| 44933 | 2013-12-14 15:01:01 |     4384 |    5 |
| 44928 | 2013-12-14 14:31:01 |     4384 |    5 |
| 44923 | 2013-12-14 14:01:01 |     4384 |    5 |
| 44918 | 2013-12-14 13:31:01 |     4384 |    5 |
| 44913 | 2013-12-14 13:01:01 |     4384 |    5 |
| 44908 | 2013-12-14 12:31:01 |     4383 |    5 |
| 44903 | 2013-12-14 12:01:01 |     4383 |    5 |
| 44898 | 2013-12-14 11:31:02 |     4383 |    5 |
| 44893 | 2013-12-14 11:01:02 |     4381 |    5 |
| 44888 | 2013-12-14 10:31:01 |     4380 |    5 |
| 44883 | 2013-12-14 10:01:01 |     4379 |    5 |
| 44878 | 2013-12-14 09:31:02 |     4379 |    5 |
| 44873 | 2013-12-14 09:01:01 |     4379 |    5 |
| 44868 | 2013-12-14 08:31:01 |     4379 |    5 |
| 44863 | 2013-12-14 08:01:02 |     4378 |    5 |
| 44858 | 2013-12-14 07:31:01 |     4378 |    5 |
| 44853 | 2013-12-14 07:01:11 |     4378 |    5 |
| 44848 | 2013-12-14 06:31:01 |     4378 |    5 |
| 44843 | 2013-12-14 06:01:01 |     4377 |    5 |
| 44838 | 2013-12-14 05:31:01 |     4377 |    5 |
| 44833 | 2013-12-14 05:01:01 |     4377 |    5 |
| 44828 | 2013-12-14 04:31:01 |     4377 |    5 |
| 44823 | 2013-12-14 04:01:01 |     4377 |    5 |
| 44818 | 2013-12-14 03:31:02 |     4377 |    5 |
| 44813 | 2013-12-14 03:01:02 |     4377 |    5 |
| 44808 | 2013-12-14 02:31:01 |     4377 |    5 |
| 44803 | 2013-12-14 02:01:01 |     4379 |    5 |
| 44798 | 2013-12-14 01:31:01 |     4379 |    5 |
| 44793 | 2013-12-14 01:01:01 |     4379 |    5 |
| 44788 | 2013-12-14 00:31:01 |     4379 |    5 |
| 44783 | 2013-12-14 00:01:01 |     4379 |    5 |
| 44778 | 2013-12-13 23:31:01 |     4380 |    5 |
| 44773 | 2013-12-13 23:01:01 |     4380 |    5 |
| 44768 | 2013-12-13 22:31:01 |     4380 |    5 |
| 44763 | 2013-12-13 22:01:01 |     4380 |    5 |
| 44758 | 2013-12-13 21:31:01 |     4380 |    5 |
| 44753 | 2013-12-13 21:01:01 |     4380 |    5 |
| 44748 | 2013-12-13 20:31:01 |     4380 |    5 |
| 44743 | 2013-12-13 20:01:01 |     4380 |    5 |
| 44738 | 2013-12-13 19:31:01 |     4380 |    5 |
| 44733 | 2013-12-13 19:01:01 |     4380 |    5 |
| 44728 | 2013-12-13 18:31:01 |     4380 |    5 |
| 44723 | 2013-12-13 18:01:01 |     4380 |    5 |
| 44718 | 2013-12-13 17:31:01 |     4380 |    5 |
| 44713 | 2013-12-13 17:01:01 |     4380 |    5 |
| 44708 | 2013-12-13 16:31:01 |     4380 |    5 |
| 44703 | 2013-12-13 16:01:01 |     4380 |    5 |
| 44698 | 2013-12-13 15:31:01 |     4380 |    5 |
| 44693 | 2013-12-13 15:01:02 |     4380 |    5 |

该表格有一个ID(这是一个自动增量),MeasuredTime是测量完成时的时间值,Measured是被测量的值What {1}}这是所采用的测量类型的int值。

我想比较两个时间顺序值,只返回差值,而不是值。我目前正在perl中进行此操作,但我想在SQL中使用它来减轻代码负载,我相信它也会更有效。

这是我使用的代码,我跳过了刚刚执行的SQL:

SELECT ID, UNIX_TIMESTAMP(MeasuredTime), Measured, What
FROM Stats
WHERE What  = 5 AND 
MeasuredTime > DATE_SUB(NOW(), INTERVAL 24 HOUR) ORDER BY ID DESC

代码:

my @array; // Contains %Hash
my @new_array;
for(my $pos = 0; $pos < scalar @array - 1; $pos++) {
 my %CurrentItem = %{$array[$pos]};
 my %NextItem = %{$array[$pos+1]};

 my %NewItem;
 $NewItem{Measured} = ($CurrentItem{Measured} - $NextItem{Measured});
 $NewItem{MeasuredTime} = ($CurrentItem{MeasuredTime} + $NextItem{MeasuredTime}) / 2; # So we average the time somewhere in the middle
 $NewItem{What} = 5;
 $NewItem{ID} = $pos; # The id value is less important here

 push @new_array, \%NewItem;
}

# At this point @new_array has the diff between the two rows, and has 1 less item than 
#  before

2 个答案:

答案 0 :(得分:3)

您可以使用user variables

SELECT   ID, UNIX_TIMESTAMP(MeasuredTime), Measured, What,
         Measured - @prev AS diff, @prev := Measured
FROM     Stats, (SELECT @prev := NULL) AS init
WHERE    What = 5
     AND MeasuredTime > NOW() - INTERVAL 24 HOUR
ORDER BY ID DESC

sqlfiddle上查看。

答案 1 :(得分:0)

试试这个

SELECT A.* ,(A.Measured - B.Measured) AS difference
FROM `stats` A CROSS JOIN `stats` B
WHERE B.ID IN (SELECT MIN(C.ID) FROM `stats` C WHERE C.ID > A.ID)
ORDER BY A.ID ASC

Fiddle