有人可以帮我理解这个查询到底是做什么的吗?
SELECT pp.Sedol
,MAX(MAX(Id)) OVER (
PARTITION BY pp.Sedol
,MAX(pp.ValueDate)
) PriceId
FROM Prices pp
GROUP BY pp.Sedol
有人可以帮我理解这个查询到底是做什么的吗?
SELECT pp.Sedol
,MAX(MAX(Id)) OVER (
PARTITION BY pp.Sedol
,MAX(pp.ValueDate)
) PriceId
FROM Prices pp
GROUP BY pp.Sedol
with x as (
select
Sedol,
max(id) max_id,
Max(ValueDate) max_valuedate
from
Prices
group by
Sedol
) select
Sedol,
max(max_id) over (partition by Sedol, max_valuedate) PriceId
from
x;
虽然就像Lamak所说的,我看不出这不会只是相当于
SELECT Sedol, MAX(Id) PriceId FROM Prices GROUP BY Sedol
declare @Prices table (Id int, ValueDate datetime, Sedol int)
Insert into @Prices values (1,'2014-09-06' ,200),
(2,'2014-09-07' , 100),
(3,'2014-09-08' , 100),
(4,'2014-09-09' , 100),
(5,'2014-09-10' , 300),
(6,'2014-09-11' , 300),
(7,'2014-09-12' , 100),
(8,'2014-09-13' , 200),
(9,'2014-09-14' , 200),
(10,'2014-09-15' , 200)
Select * from @Prices
-- Your SQL
SELECT pp.Sedol
,MAX(MAX(Id)) OVER (
PARTITION BY pp.Sedol
,MAX(pp.ValueDate)
) PriceId
FROM @Prices pp
GROUP BY pp.Sedol
-- Simple SQL mentioned by Lamak
SELECT Sedol, MAX(Id) PriceId FROM @Prices GROUP BY Sedol
结果集: