如何显示第一列中的值完成第二列缺失值的列?
我想要这个结果(红色标记列):
答案 0 :(得分:3)
Select Column1, Column2, Coalesce(column2, Column1)
假设你关心的只是column2,除非在column1中有一个值,此时你想要column1。
你的问题不是很清楚。
答案 1 :(得分:1)
使用CASE
:
SELECT COL1 , COL2 , COL3 = CASE WHEN COL1 IS NOT NULL THEN COL1 ELSE COL2 END
FROM YOURTABLE;
使用coalesce
:
SELECT * ,coalesce(COL1,COL2) AS COL3
FROM YOURTABLE;
答案 2 :(得分:0)
你可以使用如下案例:
Select *, ThirdColumn = Case when SecondColumn is null then FirstColumn else SecondColumn End
FourthColumn = Row_Number() over(partition by coalesce(SecondColumn,FirstColumn) order by Id)
from yourtable
或合并
Select *, ThirdColumn = coalesce(SecondColumn,FirstColumn)
FourthColumn = Row_Number() over(partition by coalesce(SecondColumn,FirstColumn) order by Id)
from yourtable