問題:我可以不顯示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
當您運行 「代碼」 會發生什麼。你期望發生什麼?你問的實際問題是什麼?看起來像你有一個很好的問題,只是沒有具體... –
我實際上開始的問題,我有perl代碼的作品,這不是問題,我想perl代碼成爲SQL代碼,返回差異 – nrathaus