从结果集中删除空白行

时间:2015-05-07 11:05:38

标签: sql sql-server

我有这样的查询:

select
    '0' as F_Stand_Code,
    '' as F_Stand_Desc
union all
select
    F_Stand,
    F_Stand
from T_ExhibitorLocation
where F_Site ='DIP2'
  and F_Bld = 'Le Meridien'
  and F_Hall = 'Great Ballroom'
  and F_ExhibitionCode='10991'

执行此操作时,我在结果集中得到一些空白行:

SSMS resultset

3 个答案:

答案 0 :(得分:1)

这是您的查询:

select '0' as F_Stand_Code,'' as F_Stand_Desc
union
Select F_Stand, F_Stand 
from T_ExhibitorLocation 
where F_Site = 'DIP2' and F_Bld = 'Le Meridien' and
     F_Hall = 'Great Ballroom' and F_ExhibitionCode = '10991'

我的第一个回答是,如果你不想要一个空白行,那么就不要包含它。只需使用:

Select F_Stand, F_Stand 
from T_ExhibitorLocation 
where F_Site = 'DIP2' and F_Bld = 'Le Meridien' and
      F_Hall = 'Great Ballroom' and F_ExhibitionCode = '10991';

我的第二个是使用order by(我建议union all):

select t.*
from (select '0' as F_Stand_Code, '' as F_Stand_Desc
      union all
      Select F_Stand, F_Stand 
      from T_ExhibitorLocation 
      where F_Site = 'DIP2' and F_Bld = 'Le Meridien' and
            F_Hall = 'Great Ballroom' and F_ExhibitionCode = '10991'
     ) t
order by (case when F_Stand_Code = '0' then 1 else 0 end);

通常,如果您希望按特定顺序排列结果,则需要包含order by。无法保证结果集的排序。除非您使用order by

答案 1 :(得分:1)

我不知道你得到了什么结果,但试试这个我的想法

select * 
from (
   select '0' as F_Stand_Code,'' as F_Stand_Desc union Select F_Stand,F_Stand 
   from T_ExhibitorLocation 
   where F_Site ='DIP2' and F_Bld = 'Le Meridien' 
   and F_Hall = 'Great Ballroom' and F_ExhibitionCode='10991'
) q
where q.<column> ........

然后使用where子句将过滤器空白记录添加到结果集

答案 2 :(得分:0)

不完全确切知道您的结果图片是空白的,但不应该看起来像这样

SELECT '0' AS F_Stand_Code, '' AS F_Stand_Desc 
UNION ALL 
SELECT F_Stand_Code, F_Stand_Desc
FROM T_ExhibitorLocation 
WHERE F_Site ='DIP2' 
AND F_Bld = 'Le Meridien' 
AND F_Hall = 'Great Ballroom'
AND F_ExhibitionCode='10991'