如果整列中有空值,则从输出中删除

时间:2015-11-05 07:19:14

标签: sql sql-server database

我不确定是否有可能,但感谢任何人可以帮助我。

如果所有输出行的列都为空,则不应出现在输出中。

示例数据:

DECLARE @T TABLE
(

BaseVehicle VARCHAR (50),   
SubModel    VARCHAR (50),
Make    VARCHAR (50),
Years   VARCHAR (50),
FromYear VARCHAR (50),  
ToYear  VARCHAR (50)
)


INSERT @T

SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL     
SELECT '1979 Ford LTD','','FORD','1979','','2005' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','','' 

预期产出

BaseVehicle   |Make|year|toyear
1979 Ford LTD |ford|1979|
1979 Ford LTD |ford|1979|2005
1979 Ford LTD |ford|1979|
1979 Ford LTD |ford|1979|

由于

3 个答案:

答案 0 :(得分:1)

实际上你可以用临时表来做这件事,因为你可以改变它。但我不建议这样做:

DECLARE @T TABLE(
BaseVehicle VARCHAR (50),   
SubModel    VARCHAR (50),
Make    VARCHAR (50),
Years   VARCHAR (50),
FromYear VARCHAR (50),  
ToYear  VARCHAR (50))

INSERT @T
SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL     
SELECT '1979 Ford LTD','','FORD','1979','','2005' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','','' 

SELECT * INTO #staging FROM @T

IF(NOT EXISTS(SELECT * FROM #staging WHERE BaseVehicle <> ''))
    ALTER TABLE #staging DROP COLUMN BaseVehicle

IF(NOT EXISTS(SELECT * FROM #staging WHERE SubModel <> ''))
    ALTER TABLE #staging DROP COLUMN SubModel

IF(NOT EXISTS(SELECT * FROM #staging WHERE Make <> ''))
    ALTER TABLE #staging DROP COLUMN Make

IF(NOT EXISTS(SELECT * FROM #staging WHERE Years <> ''))
    ALTER TABLE #staging DROP COLUMN Years

IF(NOT EXISTS(SELECT * FROM #staging WHERE FromYear <> ''))
    ALTER TABLE #staging DROP COLUMN FromYear

IF(NOT EXISTS(SELECT * FROM #staging WHERE ToYear <> ''))
    ALTER TABLE #staging DROP COLUMN ToYear

SELECT * FROM #staging
DROP TABLE #staging

输出:

BaseVehicle     Make    Years   ToYear
1979 Ford LTD   FORD    1979    
1979 Ford LTD   FORD    1979    2005
1979 Ford LTD   FORD    1979    
1979 Ford LTD   FORD    1979    

答案 1 :(得分:1)

任何给定SQL查询中的列数都是静态的。没有语法可以动态添加或删除SQL查询中的列。

您可以将执行两个查询的内容混合在一起:首先确定一个或多个行是否有值,然后第二次执行两个不同的SELECT之一,一个包含列表中的额外列而另一个不包含。

类似的东西:

IF EXISTS(SELECT * FROM @T WHERE Submodel <> '')
    SELECT BaseVehicle, Submodel, Make, Year, FromYears, ToYear FROM @T
ELSE
    SELECT BaseVehicle, Make, Year, FromYears, ToYear FROM @T;

正如您可能猜到的,这是非常hacky而不是一般解决方案。因此,如果你需要多个列和/或表格,那么它将变得非常难以管理。

这实际上听起来更适合应用层。无论运行查询和处理结果的代码是什么,更适合显示和隐藏列。

答案 2 :(得分:1)

检查全部计数为null的单元格数。如果两者匹配,则整个列为null。然后从选择中留下该列。

<强>查询

create table #T
(
   BaseVehicle VARCHAR (50),   
   SubModel    VARCHAR (50),
   Make    VARCHAR (50),
   Years   VARCHAR (50),
   FromYear VARCHAR (50),  
   ToYear  VARCHAR (50)
)

INSERT into #T 
SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL     
SELECT '1979 Ford LTD','','FORD','1979','','2005' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','','' UNION ALL         
SELECT '1979 Ford LTD','','FORD','1979','',''

declare @strsql varchar(2500)
  set @strsql = 'select '
  set @strsql += 
  (select case when  (select COUNT(*) from #T where BaseVehicle = '') 
  <> (select count(*) from #T ) then 'BaseVehicle, ' else '' end)
  set @strsql += 
  (select case when  (select COUNT(*) from #T  where SubModel = '') 
  <> (select count(*) from #T ) then 'SubModel, ' else '' end)
  set @strsql += 
  (select case when  (select COUNT(*) from #T  where Make = '') 
  <> (select count(*) from #T ) then 'Make, ' else '' end)
  set @strsql += 
  (select case when  (select COUNT(*) from #T  where Years = '') 
  <> (select count(*) from #T ) then 'Years, ' else '' end)
  set @strsql += 
  (select case when  (select COUNT(*) from #T  where FromYear = '') 
  <> (select count(*) from #T ) then 'FromYear, ' else '' end)
  set @strsql += 
  (select case when  (select COUNT(*) from #T  where ToYear = '') 
  <> (select count(*) from #T ) then 'ToYear, ' else '' end)
  set @strsql = LEFT(@strsql,len(@strsql) -1)
  set @strsql += ' from #T'

  exec (@strsql)

<强>结果

BaseVehicle     Make    Years   ToYear
1979 Ford LTD   FORD    1979    
1979 Ford LTD   FORD    1979    2005
1979 Ford LTD   FORD    1979    
1979 Ford LTD   FORD    1979    

如果表格位于INFORMATION_SCHEMA

,您甚至可以动态地进行查询