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

如何从列中选取单个值并使用PARTITION BY在另一列中复制该值?

Fixing the NewHeight Column Calculation

The problem with your current query is that the case when Height is not null condition limits the NewHeight value to only rows where Height isn't null. For rows where Height is null, NewHeight stays null—this doesn’t match your desired output where every row in the same Name group gets the non-null Height value.

To fix this, simply remove the case statement. The MAX(Height) OVER(PARTITION BY Name) expression automatically ignores null values and returns the non-null Height for all rows in the same Name group, even when the current row’s Height is null.

Corrected Query:

SELECT Name, Height, MAX(Height) OVER(PARTITION BY Name) AS NewHeight 
FROM MyTable;

Expected Output:

NameHeightNewHeight
Johny5.65.6
JohnyNULL5.6
JohnyNULL5.6
Mike6.16.1
MikeNULL6.1
MikeNULL6.1

Why This Works:

The MAX() aggregate function skips NULL values, so when grouped by Name, it picks up the only non-null Height value for each group. The OVER(PARTITION BY Name) clause ensures this value is applied to every row in the group, regardless of whether the individual row’s Height is null or not.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:04:44