我有一个非常简单的表。它有一个关键列和另外4列表示不同的数据类型。规则是这样的:SQL Server 2008 R2 - 组合结果
- 每一行都有一个完整的键值
- 对于每一行其余4列可能是所有空
或 - 对于每一行仅存的4列1将填充
我想要实现的是写一个查询,这将导致只有两列(键和4的1)。基本上凝结4列ito之一。
的4列的数据类型如下:
nvarchar(255)
numeric(32, 5)
datetime
nvarchar(MAX)
源数据
╔═════╦═══════════╦══════╦════════════╦════════════════╗
║ key ║ col1 ║ col2 ║ col3 ║ col4 ║
╠═════╬═══════════╬══════╬════════════╬════════════════╣
║ 1 ║ some text ║ null ║ null ║ null ║
║ 2 ║ null ║ 5 ║ null ║ null ║
║ 3 ║ null ║ null ║ null ║ null ║
║ 4 ║ null ║ null ║ 23/02/2017 ║ null ║
║ 5 ║ null ║ null ║ null ║ much more text ║
╚═════╩═══════════╩══════╩════════════╩════════════════╝
理想输出
╔═════╦════════════════╗
║ key ║ newCol ║
╠═════╬════════════════╣
║ 1 ║ some text ║
║ 2 ║ 5 ║
║ 3 ║ null ║
║ 4 ║ 23/02/2017 ║
║ 5 ║ much more text ║
╚═════╩════════════════╝
任何帮助理解。
样本数据和期望的结果真的有帮助。你想要什么样的输出?结果集中的列只有一种类型。 –