我正在尝试根据一组software_image_build_id and variant
这是我的SQL小提琴链接http://sqlfiddle.com/#!9/6ed643/1,它应该为software_image_build_id和variant的组合输出最大时间戳行,我尝试如下但是没有帮助,任何人都可以提供有关如何修复它的指导吗?
SELECT software_image_build,MAX(timestamp) FROM software_image_builds where software_image_id=1452
group by software_image_build
答案 0 :(得分:0)
您需要使用内部查询来获取数据以及最大时间戳,然后使用外部查询通过software_image_id和variant获取数据,以确保您获得具有最大时间戳的正确数据。
SELECT OUTSIDE.*
FROM software_image_builds OUTSIDE,
(SELECT software_image_id, variant, MAX(timestamp) as maxtimestamp
FROM software_image_builds
GROUP BY software_image_id, variant) AS INSIDE
WHERE OUTSIDE.software_image_id = INSIDE.software_image_id
AND OUTSIDE.variant = INSIDE.variant
AND OUTSIDE.timestamp = INSIDE.maxtimestamp
答案 1 :(得分:0)
结果似乎正确,你想要什么结果? 这个sql获取software_image_build和每个software_image_build的最大时间戳。
如果要获取所有数据的最大时间戳,请不要使用group by。
SELECT software_image_build,MAX(timestamp) FROM software_image_builds where software_image_id=1452
如果您想获取每个software_image_build,并将每行的最大时间戳拆分,请尝试以下查询:
SELECT S.software_image_build,A.max_timestamp
FROM software_image_builds AS S,
(SELECT software_image_build,MAX(timestamp) as max_timestamp
FROM software_image_builds where software_image_id=1452)
AS A
group by S.software_image_build
输出:
software_image_build max_timestamp
CI_LA.UM.5.8-02200-8x98.1-16 2017-07-14T21:51:02Z
CI_LA.UM.5.8-02200-8x98.1-20 2017-07-14T21:51:02Z
CI_LA.UM.5.8-60402-8x98.1-1 2017-07-14T21:51:02Z
答案 2 :(得分:0)
您现在发布的SQL:
SELECT software_image_build,max(timestamp)
FROM software_image_builds
WHERE software_image_id=1452
GROUP BY software_image_build,variant
LIMIT 1
输出:
software_image_build timestamp
CI_LA.UM.5.8-02200-8x98.1-16 2017-07-13T18:38:41Z
CI_LA.UM.5.8-02200-8x98.1-20 2017-07-14T21:51:02Z
CI_LA.UM.5.8-60402-8x98.1-1 2017-02-28T15:08:10Z
这似乎正是你现在所说的你想要的。如果没有,则需要进一步澄清。
答案 3 :(得分:0)
试试这个
SELECT * FROM software_image_builds where software_image_id=1452
AND timestamp = (SELECT MAX(timestamp) as timestamp from
software_image_builds
where software_image_id=1452)
答案 4 :(得分:-1)
select * from software_image_builds where timestamp in(SELECT MAX(timestamp) 来自software_image_builds group by software_image_id,variant);
答案 5 :(得分:-1)
您可以按时间戳列对结果进行排序,然后使用limit仅返回第一个结果。
SELECT software_image_build, timestamp FROM software_image_builds where
software_image_id=1452 group by software_image_build order by timestamp DESC
limit 1