You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在SQL中为每个ID分组计算并展示最大Outstanding值

SQL Query to Add MAX_OUTSTANDING Column per ID

Got it, here's a straightforward solution using SQL window functions to get exactly the result you need:

Original Table (Table1)

outstandingmonthyearID
50000000720171234
40000000820171234
60000000920171234
45000001020171234
5000000820171235
7000000920171235
45000000720171236
30000000820171236
48000000920171236
45000001020171236

Requirement

Query all fields from Table1, and add a MAX_OUTSTANDING column that shows the maximum value of outstanding for each corresponding ID.

Solution Query

SELECT 
    outstanding,
    month,
    year,
    ID,
    MAX(outstanding) OVER (PARTITION BY ID) AS MAX_OUTSTANDING
FROM Table1;

How This Works

The MAX(outstanding) OVER (PARTITION BY ID) part is a window function:

  • MAX(outstanding) calculates the highest value in the outstanding column
  • PARTITION BY ID splits the data into groups based on each unique ID, so the max is calculated per ID
  • Unlike a regular GROUP BY which would collapse rows into a single row per group, this window function preserves all original rows while attaching the correct max value to every row in the group.

Expected Result

OUTSTANDINGMONTHYEARIDMAX_OUTSTANDING
500000007201712346000000
400000008201712346000000
600000009201712346000000
450000010201712346000000
5000000820171235700000
7000000920171235700000
450000007201712364800000
300000008201712364800000
480000009201712364800000
450000010201712364800000

内容的提问来源于stack exchange,提问作者mae

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:23:09