我想为每个ID选择最高价格,但只选择具有最高lastupdated列的行的价格。
使用此查询不会给我正确的价格。
SELECT
id, MAX(price) as price, MAX(lastupdated) as lastupdated
FROM
tbl
GROUP BY
id
示例tbl数据:
id, price, lastupdated
1, 50, 2015-03-01
1, 51, 2015-03-01
1, 52, 2015-03-01
1, 53, 2015-02-28
1, 54, 2015-02-28
1, 55, 2015-02-28
2, 20, 2015-02-01
2, 21, 2015-02-01
2, 22, 2015-02-01
2, 33, 2015-01-28
2, 34, 2015-01-28
2, 35, 2015-01-28
我喜欢的结果是:
id, price, lastupdated
1, 52, 2015-03-01
2, 22, 2015-02-01
答案 0 :(得分:3)
您可以使用RANK
为表格的每条记录指定排名编号。然后在外部查询中使用此数字,以选择每lastupdated
个id
值最新的所有记录:
SELECT id, MAX(price) as price, lastupdated
FROM (
SELECT id, price, lastupdated,
RANK() OVER (PARTITION BY id ORDER BY lastupdated DESC) AS rnk
FROM tbl ) t
WHERE t.rnk = 1
GROUP BY id, lastupdated
或者,您可以使用HAVING
子句过滤掉任何(id
,lastupdated
)
没有最大lastupdated
日期值的组:
SELECT id, MAX(price) as price, lastupdated
FROM #tbl t1
GROUP BY id, lastupdated
HAVING lastupdated = (SELECT MAX(lastupdated) FROM #tbl t2 WHERE id = t1.id)
使用HAVING
子句的查询更加简洁但效率较低,因为它使用的子查询(SELECT MAX(lastupdated) ...
)在外部查询中定义的每个的评估一次
答案 1 :(得分:1)
您可以使用过滤join
来查看具有最新日期的行:
SELECT tbl.id
, MAX(price) as price
, lastupdated
FROM Tel
JOIN (
SELECT id
, MAX(last_updated) as maxupdated
FROM tbl
GROUP BY
id
) mx
ON tbl.id = mx.id
AND tbl.lastupdated = mx.maxupdated
GROUP BY
tbl.id
, last_updated
答案 2 :(得分:1)
您需要找到每个id的lastupdated并加入:
SELECT tbl.id, MAX(price) as price, m.lastupdated
FROM tbl
JOIN (
SELECT id, MAX(lastupdated) AS lastupdated
FROM tbl
GROUP BY id
) m ON tbl.id = m.id and tbl.lastupdated = m.lastupdated
GROUP BY tbl.id, m.lastupdated
ORDER BY tbl.id
答案 3 :(得分:1)
今天我觉得cte是一个很好的方法
with cte ( id, maxdate )
as
(
select id, max(lastupdated)
from tbl
group by id
)
select
t3.id
,max(price)
,max(maxdate)
from
tbl t3
inner join cte on t3.id = cte.id and t3.lastupdated = cte.maxdate
group by
t3.id