我的表中有这些数据:
onum amt odate cnum snum
3001 18,69 1990-03-10 00:00:00.000 2008 1007
3002 1900,10 1990-03-10 00:00:00.000 2007 1004
3003 767,19 1990-03-10 00:00:00.000 2001 1001
3005 5160,45 1990-03-10 00:00:00.000 2003 1002
3006 1098,16 1990-03-10 00:00:00.000 2008 1007
3007 75,75 1990-03-10 00:00:00.000 2004 1002
3008 4723,00 1990-05-10 00:00:00.000 2006 1001
3009 1713,23 1990-04-10 00:00:00.000 2002 1003
3010 1309,95 1990-06-10 00:00:00.000 2004 1002
3011 9891,88 1990-06-10 00:00:00.000 2006 1001
我需要得到这个结果:
amt odate snum
5160,45 1990-03-10 00:00:00.000 1002
4723,00 1990-05-10 00:00:00.000 1001
9891,88 1990-06-10 00:00:00.000 1001
即我在amt
每天选择最大odate
,但显示卖家snum
如果我写这个:
SELECT MAX(amt), odate, snum
FROM [Understanding].[dbo].[Orders]
GROUP BY odate, snum
输出错误,因为它按天和卖家显示组。
答案 0 :(得分:4)
;with C as
(
select amt,
odate,
snum,
row_number() over(partition by odate order by amt desc) as rn
from Orders
)
select amt,
odate,
snum
from C
where rn = 1
答案 1 :(得分:0)
select a.*,b.snum
from
(
select MAX(amt) as amt
,odate
from Orders
group by odate
)A INNER JOIN Orders B
ON A.amt = b.amt
order by a.odate