我有一张MyTable
这样的表:
Date Qty Price 2017-07-01 2 5.00 2017-07-08 3 4.00 2017-08-08 1 6.00
如果我进行查询以获得最大总成本,如下所示:
SELECT max(Qty * Price) FROM MyTable
我明白12.00
哪个是正确的。
我如何获得相应的日期,即 2017-07-08?如果有几个相等的最大值,我想要最小(最旧)的日期。
create table MyTable (date datetime, qty int, price int)
go
insert into MyTable(date,qty,price)
values
('2017-07-01', 2, 5.00),
('2017-07-08', 3, 4.00),
('2017-08-08', 1, 6.00);
with q as
(
select *, row_number() over (order by qty*price desc) rn
from MyTable
)
select date, qty, price
from q
where rn = 1;
产出
date qty price
----------------------- ----------- -----------
2017-07-08 00:00:00.000 3 4
(1 row(s) affected)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句