我有一个复杂的查询,结尾如下:
PIVOT
(
MAX([Name])
FOR [Name] In([Category1], [Category2], [Category3])
) As pt
pivot函数将多行数据合并为一行。其中一些字段为空,一些字段具有类别标志。如果任何这些列具有值,如何输出单个列,如果所有值都为空,则输出为false?值为text或null。额外,如何抑制或隐藏输出中的列?我正在寻求MSSQL SQL兼容的答案。
答案 0 :(得分:1)
您将该逻辑添加到数据透视表上方的select clause
SELECT
id, [Category1], [Category2], [Category3]
, case when [Category1] is null and [Category2] is null and [Category3] is null
then 'False'
else 'True'
end your_ol_name
FROM yourtable
PIVOT
(
MAX([Name])
FOR [Name] In([Category1], [Category2], [Category3])
) As pt
请注意,同样的select子句也是在任何列上使用ISNULL()或COALESCE()的地方。我不确定你打算做什么"抑制或隐藏"。也许这会对你有所帮助:
SELECT
id
, ISNULL([Category1],'-') Category1
, ISNULL([Category2],'-') Category2
, ISNULL([Category3],'-') Category3
, case when [Category1] is null and [Category2] is null and [Category3] is null
then 'False'
else 'True'
end your_ol_name
FROM yourtable
PIVOT
(
MAX([Name])
FOR [Name] In([Category1], [Category2], [Category3])
) As pt