我正在使用 union all 来联合多个查询,但 union all 并没有为这些零值插入一个空行。我查看了该站点,根据这个问题,它应该包括它们:
这些是我的疑问:
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 1 and SMTH > 0 )
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 2 and SMTH > 0 )
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 3 and SMTH > 0 )
union all
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 4 and SMTH > 0 )
结果:
10068
3967
895
四个独立查询:
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 1 and SMTH > 0 )
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 2 and SMTH > 0 )
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 3 and SMTH > 0 )
select [SMTH] from [H].[D].[T]
where Date = (SELECT MAX(DATE) from [H].[D].[T] where Data = 4 and SMTH > 0 )
结果:
10068
3967
0
895
所以基本上因为我认为上一个问题的答案是正确的,我做错了什么?
谢谢!