查找每条记录的最大行值并将其应用于新列

Find maximum row value for each record and apply it to new column

我试图摆脱 NULL 值并创建一个新列来捕获 Y(是)。每个记录 ID 都有一个单独的最喜欢的颜色列。单个记录 ID 永远不会有 N(否)或超过 1 个 Y。

我试图在每一列上做 select 分钟,然后按记录 ID 分组,但没有成功。 如有任何帮助,我们将不胜感激!

输入:

RecordID RecordDate FavoriteColorYellow Favorite ColorGreen FavoriteColorPurple
1 1/1/2020 Y NULL NULL
2 2/8/2021 NULL NULL Y
3 2/10/2020 NULL Y NULL
4 3/1/2020 Y NULL NULL

预期输出:

RecordID RecordDate FavoriteColor
1 1/1/2020 Yellow
2 2/8/2021 Purple
3 2/10/2020 Green
4 3/1/2020 Yellow

下面的代码让您可以重现表格。

CREATE TABLE Records (
    RecordID int,
    RecordDate datetime);

CREATE TABLE Colors (
    RecordID int,
    FavoriteColorYellow varchar(1),
    FavoriteColorGreen varchar(1), 
    FavoriteColorPurple varchar(1), 
    );

INSERT INTO Records (RecordID, RecordDate)
VALUES (1, '1/1/2020'),
(2, '2/8/2020'),
(3, '2/10/2020'),
(4, '3/1/2020')

INSERT INTO Colors(RecordID, FavoriteColorYellow, FavoriteColorGreen, FavoriteColorPurple)
VALUES (1, 'Y', NULL, NULL),
(2, NULL, NULL, 'Y'),
(3, NULL, 'Y', NULL),
(4, 'Y', NULL, NULL)

我使用 CASE 尝试过类似的操作,但我的实际数据集大约有 10 列,写出所有组合似乎有点混乱:

SELECT 
T1.[RecordID]
,T1.[RecordDate]
--,T2.[FavoriteColorYellow]
--,T2.[FavoriteColorGreen]
--,T2.[FavoriteColorPurple]
,CASE
    WHEN (T2.[FavoriteColorYellow] IS NULL OR T2.[FavoriteColorGreen] IS NULL) THEN T2.[FavoriteColorPurple]
    WHEN (T2.[FavoriteColorPurple] IS NULL OR T2.[FavoriteColorYellow] IS NULL) THEN T2.[FavoriteColorGreen]
    ELSE '0'
    END AS FAV

FROM [Records] T1
LEFT JOIN [Colors] as T2 ON T1.[RecordID] = T2.[RecordID]

case 可让您根据需要在各列之间切换并折叠为单个值。

case 'Y'
    when FavoriteColorGreen  then 'Green'
    when FavoriteColorPurple then 'Purple'
    when FavoriteColorYellow then 'Yellow'
    else 'NA' -- optional
end as FavoriteColor