1

我想创建一个名为 NewHeight 的新列,它采用在 Height 列中找到的单个值并将其复制为所有相同的名称。这是我正在使用的查询。

Select 
Name,
Height,
case when Height is not null then max(Height) over(partition by Name) end as NewHeight
from MyTable

这是我的输出:

Name    Height  NewHeight
Johny       
Johny   5.6      5.6
Johny       
Mike        
Mike        
Mike    6.1      6.1

这是所需的输出:

Name    Height  NewHeight
Johny            5.6
Johny   5.6      5.6
Johny            5.6
Mike             6.1
Mike             6.1
Mike    6.1      6.1
4

2 回答 2

4

is not null在应该使用的时候使用is null。但是,coalesce()更简洁:

Select Name, Height,
       coalesce(Height, max(Height) over (partition by Name) end as NewHeight
from MyTable;

或者,如果您希望每一行都具有相同的值,您甚至不需要条件逻辑:

Select Name, Height,
       max(Height) over (partition by Name) as NewHeight
from MyTable;
于 2018-05-10T17:53:54.013 回答
0

使用coalesce()

Select  Name, Height,
        coalesce(Height, max(Height) over(partition by Name)) as NewHeight
from MyTable
于 2018-05-10T17:54:13.637 回答