2013-12-16 50 views
1

问题:我可以不显示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是测量类型的整数值。

我想比较两个时间顺序值,只是返回差异,而不是值。我目前在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 
+1

当您运行 “代码” 会发生什么。你期望发生什么?你问的实际问题是什么?看起来像你有一个很好的问题,只是没有具体... –

+0

我实际上开始的问题,我有perl代码的作品,这不是问题,我想perl代码成为SQL代码,返回差异 – nrathaus

回答

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

+0

谢谢:)从来没有想过这样做 – nrathaus

+0

有趣的好解决方案,只有ID可能是一个概率 - >命令我的mesuredTime? – halfbit

+0

@halfbit:也许吧。我不知道。问题中的原始代码是用“ID”排序的,所以我认为OP知道他们在那里做了什么。显然,如果需要不同的排序,则应该应用它。 – eggyal

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

相关问题