我尝试在phpmyadmin和sql小提琴中输入查询。 我认为时区与此有关。其中一个转换时考虑了时区而另一个没有考虑。 结果也不同。对于小提琴,结果是这样的。
ORDR DAY COUNT AGGREGATE AVERAGE PREVIOUS PERIOD VOTE VELOCITY (COALESCE(T_1.COUNT, 0) - COALESCE(T_2.COUNT, 0)) * 100.0/ COALESCE (T_2.COUNT, 0)
16 August, 11 2012 1 1 0 (null)
17 August, 12 2012 1 2 0.0625 (null)
18 August, 13 2012 4 9 0.2941 0
19 August, 14 2012 1 13 0.6667 600
这是针对phpmyadmin
ORDR DAY COUNT AGGREGATE AVERAGE PREVIOUS PERIOD VOTE VELOCITY (COALESCE(T_1.COUNT, 0) - COALESCE(T_2.COUNT, 0)) * 100.0/ COALESCE (T_2.COUNT, 0)
14 2012-08-11 1 1 0.0000 50.00000
15 2012-08-12 1 2 0.0714 -66.66667
1 2012-08-13 6 8 NULL NULL
2 2012-08-14 3 11 8.0000 NULL
这是小提琴的链接。 http://sqlfiddle.com/#!2/ef77b/106 这是phpmyadmin的查询。
drop table if exists sampleData;
create table sampleData
(
id int NOT NULL AUTO_INCREMENT,
timecode int,
count int,
PRIMARY KEY (id)
)
-- #ENGINE=MyISAM
;
INSERT INTO sampleData
(timecode, count)
VALUES
(1344893440, 1), ( 1346014720, 1),( 1344898688,1),( 1345654784,1),( 1345978368,1),
( 1345959296,1), (1345064704,1), ( 1345156352,1),( 1345225600,1),
(1345017984,1),( 1345640960,1),( 1346019968,1),( 1345834752,1),
( 1345438464,1),( 1344986880,1),( 1345045632,1),( 1345557888,1),( 1344973056,1),( 1345087232,1),( 1345433216,1),( 1345691008,1),
( 1344917760,1),( 1345253248,1),( 1344934912,1),( 1345890048,1),( 1345272448,1), (1345829504,1),( 1345798400,1),( 1345203200,1),( 1344741120,1),
( 1345175552,1),( 1344824192,1),( 1344926336,1),( 1345571712,1),( 1344931584,1),( 1345211776,1),( 1345059456,1),( 1345516288,1),( 1345441920,1),( 1346009472,1);
set @t3.id=0;
set @t3.id2=0; -- must differentiate the ids because if not, they will increase and will not match. That is, there will not be 3 identical columns but 1 very long column.
set @t3.id3=0;
select t.*,
(coalesce(t_1.count, 0) - coalesce(t_2.count, 0)) * 100.0/ coalesce (t_2.count, 0)
from
(SELECT @t3.id:=@t3.id+1 AS ordr,
t3.day, t3.count , SUM(t2.count) AS AGGREGATE , (SUM(t2.count)-t3.count)/(@t3.id) AS "average previous period vote velocity"
-- (count(t3.count)-1) as "number of people voted",
FROM
(
SELECT t1.id, t1.hour, t1.day, sum(t1.count ) AS COUNT
FROM
(
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t1
GROUP BY t1.DAY -- no need count
HAVING COUNT(*)>0
ORDER BY t1.DAY
) t3
INNER JOIN
(
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t2
ON t3.DAY>=t2.DAY
GROUP BY t3.DAY, t3.COUNT
ORDER BY t3.DAY)t
left outer join
(SELECT @t3.id2:=@t3.id2+1 AS ordr,
t3.day, t3.count , SUM(t2.count) AS AGGREGATE , (SUM(t2.count)-t3.count)/(@t3.id2) AS "average previous period vote velocity"
-- (count(t3.count)-1) as "number of people voted",
FROM
(
SELECT t1.id as id2, t1.hour, t1.day, sum(t1.count ) AS COUNT
FROM
(
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t1
GROUP BY t1.DAY -- no need count
HAVING COUNT(*)>0
ORDER BY t1.DAY
) t3
INNER JOIN
(
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t2
ON t3.DAY>=t2.DAY
GROUP BY t3.DAY, t3.COUNT
ORDER BY t3.DAY) t_1
on t.ordr = t_1.ordr + 1
left outer join
(SELECT @t3.id3:=@t3.id3+1 AS ordr,
t3.day, t3.count , SUM(t2.count) AS AGGREGATE , (SUM(t2.count)-t3.count)/(@t3.id3) AS "average previous period vote velocity"
-- (count(t3.count)-1) as "number of people voted",
FROM
(
SELECT t1.id as id3, t1.hour, t1.day, sum(t1.count ) AS COUNT
FROM
(
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT id, hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t1
GROUP BY t1.DAY -- no need count
HAVING COUNT(*)>0
ORDER BY t1.DAY
) t3
INNER JOIN
(
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode)) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))>=14
GROUP BY DAY)
UNION ALL
(SELECT hour(time(FROM_UNIXTIME( timecode))) AS hour,
date(FROM_UNIXTIME( timecode-86400) ) AS DAY,
(FROM_UNIXTIME( timecode)) AS original, COUNT(1) AS 'count'
FROM sampleData
WHERE hour(time(FROM_UNIXTIME( timecode)))< 14
GROUP BY DAY)
) t2
ON t3.DAY>=t2.DAY
GROUP BY t3.DAY, t3.COUNT
ORDER BY t3.DAY) t_2
on t.ordr = t_2.ordr + 2