我能想到几条路线,没有一条一定很漂亮......
人们经常找到第一个,然后寻找更好的东西。我不确定是否真的有更好的东西。
SELECT
ColumnA,
CASE WHEN ColumnA IS NULL THEN Column1 ELSE Column2 END AS ColumnB,
CASE WHEN ColumnA IS NULL THEN Column3 ELSE Column4 END AS ColumnC
FROM
yourTable
或者...
SELECT
yourTable.ColumnA,
subTable.ColumnB,
subTable.ColumnC
FROM
yourTable
CROSS APPLY
(
SELECT yourTable.Column1 AS ColumnB, yourTable.Column3 AS ColumnC WHERE yourTable.ColumnA IS NULL
UNION ALL
SELECT yourTable.Column2 AS ColumnB, yourTable.Column4 AS ColumnC WHERE yourTable.ColumnA IS NOT NULL
)
AS subTable
或者...
SELECT
ColumnA,
Column1 AS ColumnB,
Column2 AS ColumnC
FROM
yourTable
WHERE
ColumnA IS NULL
UNION ALL
SELECT
ColumnA,
Column2 AS ColumnB,
Column4 AS ColumnC
FROM
yourTable
WHERE
ColumnA IS NOT NULL