我正在尝试
type
收集两个不同的Published
个对象(InReviewing
和Events
,Published
和Created
之间的中间时间,这样我就可以返回一个数组,其中包含日期,以及每天共享InReviewing
的任何类型Published
和target_id
之间的中等时差。我认为SQL会返回几秒钟?
示例:
[[Mon, 03 Dec 2012, 110000], [Thu, 13 Dec 2012, 2200000]]
我的原始SQL语法缺乏一些结论。这就是我到目前为止所做的:
SELECT target_id, DATE(created_at), COUNT(*)
FROM ( SELECT target_id, DATE(created_at)
FROM events
WHERE created_at > '#{@start_time}'
AND events.type = 'InReviewing'
UNION ALL
SELECT target_id, DATE(created_at)
FROM events
WHERE created_at > '#{@start_time}'
AND events.type = 'Published'
) tmp
GROUP BY DATE(created_at)
不介意#{@start_time
}`,这是使用本机代码在这里实现时间,并且它正常工作..
谢谢!
答案 0 :(得分:1)
我不认为我理解你的问题,但我仍然决定试一试(因为我此时无法访问MySql,这个例子来自MS SQL):
SELECT x.target_id, x.created_at AS InReviewingCreated, y.created_at AS PublishedCreated, DATEDIFF(SECOND, x.created_at, y.created_at) AS DiffSeconds
FROM
(SELECT *
FROM Events a
WHERE a.created_at > '#{@start_time}'
AND a.type = 'InReviewing') x INNER JOIN
(SELECT *
FROM Events a
WHERE a.created_at > '#{@start_time}'
AND a.type = 'Published') y ON x.target_id = y.target_id
ORDER BY x.created_at
这将为您提供每个目标为InReview和Published时的时间戳以及它们之间的秒数。我对你想要的东西有点困惑...... 这就是我想“你想要的”:
SELECT DATEPART(DAYOFYEAR, x.created_at), AVG(DATEDIFF(SECOND, x.created_at, y.created_at))
FROM
(SELECT *
FROM Events a
WHERE a.created_at > '#{@start_time}'
AND a.type = 'InReviewing') x INNER JOIN
(SELECT *
FROM Events a
WHERE a.created_at > '#{@start_time}'
AND a.type = 'Published') y ON x.target_id = y.target_id
GROUP BY DATEPART(DAYOFYEAR, x.created_at)
ORDER BY DATEPART(DAYOFYEAR, x.created_at)
免责声明:由于我没有您的表架构,我的测试并不完全有效。如果您要共享示例设置和所需输出,那么我可以提供更好的查询。
希望这有帮助。