我有一个查询,为每小时选择MIN值:
SELECT MIN(price), HOUR(timestamp), DATE(timestamp)
FROM `scan`
GROUP BY DATE(timestamp), HOUR(timestamp)
现在我需要为每天的每个MIN(价格)选择MIN和MAX。使用临时表应该很容易,但我不确定性能是否合理。有什么其他方法可以做到吗?
答案 0 :(得分:0)
我以为你想找到这些价格的MIN和MAX小时(问题不是很清楚)。
它应该是这样的:
SELECT DATE(ta.timestamp), ta.price, MIN(HOUR(ta.timestamp)), MAX(HOUR(ta.timestamp))
FROM `scan` ta
INNER JOIN (
SELECT MIN(price) AS minprice, DATE(timestamp) AS day
FROM `scan`
GROUP BY DATE(timestamp)
) tb
ON (tb.minprice = ta.price AND tb.day = DATE(ta.timestamp))
GROUP BY DATE(ta.timestamp), ta.price
子查询每天获取MIN价格,然后主查询仅限于匹配日期和最低价格的记录。