首页  >  问答  >  正文

使用窗口函数将最高值添加为一列

我有一个如下表:

复制数据的查询:

DROP TABLE IF EXISTS units_table;
CREATE TEMP TABLE units_table
(
 Brand varchar(100),
 units numeric(38,12)
);


  INSERT INTO units_table (Brand, units)
   VALUES ('A',200),('B',0),('C',300),('D',400),('E',1500),('F',700),('G',800),('H',450);

专门使用窗口函数,我想获得表格中最高的值。如下所示:

但是当我使用时:

select

brand,
units,
FIRST_VALUE(units) OVER () as Highest

from units_table

它给出的第一个值为 0。如果我这样做,

select

brand,
units,
FIRST_VALUE(units) OVER (ORDER BY UNITS) as Highest

from units_table

抛出错误。 具体应该如何使用Window Function来解决这个问题呢?预先感谢您!

P粉043295337P粉043295337179 天前424

全部回复(2)我来回复

  • P粉616383625

    P粉6163836252024-04-05 11:32:53

    select brand,units,
     FIRST_VALUE(units) OVER (ORDER BY UNITS DESC) as Highest
    from units_table

    对我有用

    回复
    0
  • P粉959676410

    P粉9596764102024-04-05 09:38:42

    您需要MAX()窗口函数:

    SELECT brand,
           units,
           MAX(units) OVER () AS Highest
    FROM units_table;

    请参阅演示

    回复
    0
  • 取消回复