gpt4 book ai didi

SQL 查询,返回 Max(DateField) 的价格

转载 作者:行者123 更新时间:2023-12-04 18:12:05 24 4
gpt4 key购买 nike

我有这个 sql 表:

Niin char(9)
EffectiveDate Date
Price currency

NIIN 以不同的 EffectiveDate 和 Price 值重复自身,就像这样
**NIIN           EffectiveDate       Price**

012345678 6/1/2011 89.00
012345678 6/1/2012 99.00
012345678 6/1/2012 99.00
123456789 5/1/2011 77.00
123456789 5/1/2012 80.00
123456789 5/1/2012 80.00
etc....

我需要做的是返回 NIIN,以及 Max EffectiveDate 的价格,假设如果有重复的 EffectiveDate 值,价格将始终相同。

因此,从我提供的示例中,查询将返回:
012345678       99.00
123456789 80.00

谢谢你的帮助。

最佳答案

您可以为此使用 CTE:

;with cte as
(
select niin,
row_number() over(partition by niin order by effectivedate desc) rn,
price
from yourtable
)
select niin, price
from cte
where rn = 1

SQL Fiddle with Demo

关于SQL 查询,返回 Max(DateField) 的价格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12415385/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com