我有这个查询
select adate, factoryid, purchid, itemname, max(price) as price
from tableb where catnum = 9
group by adate, factoryid, purchid, itemname
order by adate, factoryid, purchid, itemname
但我想要那一行的id。所以在一个完美的世界里:
select id, adate, factoryid, purchid, itemname, max(price) as price
from tableb where catnum = 9
group by adate, factoryid, purchid, itemname
order by adate, factoryid, purchid, itemname
但我知道这不行。
所以我尝试了这个:
select id, adate, factoryid, purchid, itemname,
max(price) over(partition by adate, factoryid, purchid, itemname) as price
from tableb where catnum = 9
这不起作用。所有ID的价格都是重复的。查询结果集从4000行变为11000。
很明显,我以某种方式弄错了窗口功能。首先,我做错了什么,其次,当然,我该如何解决?
答案 0 :(得分:2)
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY adate, factoryid, purchid, itemname ORDER BY price DESC, id DESC) rn
FROM tableb
WHERE catnum = 9
) q
WHERE rn = 1
答案 1 :(得分:2)
您可以使用MAX
的窗口版本以及FIRST_VALUE,可从SQL Server 2012 +获得:
SELECT DISTINCT adate, factoryid, purchid, itemname,
MAX(price) OVER (PARTITION BY adate, factoryid, purchid, itemname) AS price,
FIRST_VALUE(id) OVER (PARTITION BY adate, factoryid, purchid, itemname
ORDER BY price DESC) AS id
FROM tableb
WHERE catnum = 9
ORDER BY adate, factoryid, purchid, itemname
答案 2 :(得分:1)
我不清楚你为什么要这个id但是...... 假设
然后这可能会起作用:
SELECT tableb.* FROM tableb
INNER JOIN
(
select adate, factoryid, purchid, itemname, max(price) as price
from tableb where catnum = 9
group by adate, factoryid, purchid, itemname
) AS grouped
ON tableb.adate = grouped.adate AND
tableb.factoryid = grouped.factoryid AND
tableb.purchid = grouped.purchid AND
tableb.itemname = grouped.itemname AND
tableb.price = grouped.price
order by tableb.adate, tableb.factoryid, tableb.purchid, tableb.itemname