gpt4 book ai didi

sql - 交叉应用与 UNPIVOT

转载 作者:行者123 更新时间:2023-12-04 16:46:05 28 4
gpt4 key购买 nike

我发现 UNPIVOT 能够自动排除具有 NULL 值的字段。但是,CROSS APPLY-VALUES 方法无法做到这一点。有谁知道如何从 CROSS APPLY-VALUES 中自动排除 NULL 值字段?比方说,如果字段 Field3 和 Field4 包含 NULL 值,则排除它们。

SELECT 
E.FieldA,
E.FieldB,
E.FieldC,
DBParam.Display,
DBParam.Value
INTO DBParam
FROM
Map_Data AS E
CROSS APPLY (VALUES (Field1, 'Field1'),
(Field2, 'Field2'),
(Field3, 'Field3'),
(Field4, 'Field4')
) AS DBParam(Value, Display)

最佳答案

不要使用 VALUES,而是使用 SELECT:

SELECT E.FieldA, E.FieldB, E.FieldC, 
DBParam.Display, DBParam.Value
INTO DBParam
FROM Map_Data E CROSS APPLY
(SELECT Value, Display
FROM (SELECT Field1 as Value, 'Field1' as Display UNION ALL
SELECT Field2, 'Field2' UNION ALL
SELECT Field3, 'Field3' UNION ALL
SELECT Field4, 'Field4'
) vd
WHERE Value is not null
) as DBParam(Value, Display);

关于sql - 交叉应用与 UNPIVOT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23600541/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com