如何使用窗口函数获取最大价格及其ID?

时间:2015-07-01 21:27:22

标签: sql tsql sql-server-2012 aggregate-functions window-functions

我有这个查询

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。

很明显,我以某种方式弄错了窗口功能。首先,我做错了什么,其次,当然,我该如何解决?

3 个答案:

答案 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但是...... 假设

  1. 你想要条件的所有ID
  2. id是tableb的PK
  3. 然后这可能会起作用:

    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