如何从ResultSet中消除NULL列

时间:2018-02-23 06:57:18

标签: sql-server



DECLARE @Doc INT ='20'
SELECT DISTINCT 

       [Name or title]
      ,[Subject]
      ,[Approval Description]
      ,[TypeOfApproval]
      ,[TypeOfExpense]
      ,[ExpenseNature]
      ,[BudgetType]
      ,[Current State]
      ,CASE
      WHEN [Current State] = 'Initiator' 
      THEN [IniSignature] 
    END AS RES
    
     ,CASE
      WHEN [Current State] = 'Initiator' 
      THEN [IniSignatureName]
     
   END AS RES1
	   
   ,CASE
      WHEN [Current State] = 'Reviewer' 
      THEN [ReviewerSignatureName]
   END AS RES2

,CASE
      WHEN [Current State] = 'Reviewer' 
      THEN [ReviewerSignature] 
   END AS RES3


	,CASE 
	 WHEN [Current State]  = 'Reviewer' 
	 THEN [IniSignatureName]
	
   END AS RES4
	
	,CASE
      WHEN [Current State] = 'Reviewer' 
      THEN [IniSignature]
   END AS RES5
	
	,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [ApprovedSignatureName] 
	 
END AS RES6

,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [ApprovedSignature] 
	 
END AS RES7

,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [ReviewerSignatureName]
	 
END AS RES8

,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [ReviewerSignature]
	 
END AS RES9

,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [IniSignatureName]
	 
END AS RES10

,CASE 
	 WHEN [Current State]  = 'Approved' 
	 THEN [IniSignature]
	 
END AS RES11

FROM 
(

SELECT DISTINCT 

       [Name or title]
      ,[Subject]
      ,[Approval Description]
      ,TypeOfApproval.[Name] AS [TypeOfApproval]
      ,TypeOfExpense.[Name] AS [TypeOfExpense]
      ,ExpenseNature.[Name] AS [ExpenseNature]
      ,BudgetType.[Name] AS [BudgetType]
      ,UblState.Name AS [Current State] 
      ,IniSignature.[ImageBits] AS IniSignature
      ,IniSignatureName.CreatorName AS IniSignatureName
      ,ReviewerSignature.ImageBits AS ReviewerSignature
      ,ReviewerSignatureName.CreatorName AS ReviewerSignatureName
      ,ApprovedSignature.ImageBits AS ApprovedSignature
      ,ApprovedSignatureName.CreatorName AS ApprovedSignatureName  
 
  FROM [SaveImage].[dbo].[UBL] Rec
  
  LEFT JOIN  [SaveImage].[dbo].[UBL_State_Changes] ON [SaveImage].[dbo].[UBL_State_Changes].[State_ID] = Rec.[State_ID]
  
  LEFT JOIN [SaveImage].[dbo].[UBL_ApprovalType] ON [SaveImage].[dbo].[UBL_ApprovalType].[UBL_ID] = Rec.[ID]
  
  LEFT JOIN [SaveImage].[dbo].[ApprovalType] TypeOfApproval ON TypeOfApproval.[ID] =  [SaveImage].[dbo].[UBL_ApprovalType].[ApprovalType_ID]
 
  LEFT JOIN [SaveImage].[dbo].[UBL_Nature of Expense] ON [SaveImage].[dbo].[UBL_Nature of Expense].[UBL_ID] = Rec.[ID]
 
  LEFT JOIN [SaveImage].[dbo].[Expense] TypeOfExpense ON TypeOfExpense.[ID] = [SaveImage].[dbo].[UBL_Nature of Expense].[Nature of Expense_ID]
  
  LEFT JOIN [SaveImage].[dbo].[UBL_Nature of Expense] NatureOfExpense ON [SaveImage].[dbo].[UBL_Nature of Expense].[UBL_ID] = Rec.[ID]
  
  LEFT JOIN [SaveImage].[dbo].[Nature of Expense] ExpenseNature ON ExpenseNature.[ID] = [SaveImage].[dbo].[UBL_Nature of Expense].[Nature of Expense_ID]
  
  LEFT JOIN [SaveImage].[dbo].[UBL_Budget Status] ON [SaveImage].[dbo].[UBL_Budget Status].[UBL_ID] = Rec.[ID]
  
  LEFT JOIN [SaveImage].[dbo].[Budget Status] BudgetType ON BudgetType.ID = [SaveImage].[dbo].[UBL_Budget Status].[Budget Status_ID]
    
  INNER JOIN [SaveImage].[dbo].[State] AS UblState ON Rec.[State_ID] = UblState.[ID]
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS Imj  ON Imj.[CreatorName] = UblState.Name
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS IniSignature ON IniSignature.CreatorName ='Initiator'
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS IniSignatureName ON [IniSignatureName].CreatorName ='Initiator'
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS ReviewerSignature ON ReviewerSignature.CreatorName ='Reviewer'
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS ReviewerSignatureName ON [ReviewerSignatureName].CreatorName ='Reviewer'
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS ApprovedSignature ON ApprovedSignature.CreatorName ='Approved'
  
  LEFT JOIN  [SaveImage].[dbo].[Img] AS ApprovedSignatureName   ON [ApprovedSignatureName].CreatorName ='Approved'
  
  
  WHERE  Rec.[ID] = (@Doc) 
  
  ) AS BaseTbl




我使用T-SQL查询,其中定义了多个JOINS和我得到的结果正是我的要求,它就像一个对象正在通过具有不同状态的工作流程,我正在获得对象状态和相关字段。对于所需的ResultSet,我使用CASE语句,但我不知道它为什么显示具有NULL的列。我想从我的最终结果集中消除NULL列。结果集的屏幕不在下面。任何帮助都会很好。

结果集 ResultSet

谢谢&问候 穆罕默德萨米你的雷曼

1 个答案:

答案 0 :(得分:1)

我可以考虑在存储过程中实现这个逻辑,而不是通过单个查询。

声明一个临时表并从您的查询中插入完整结果,并将多个连接插入到临时表中,即临时表应具有与查询返回的列数和类型相同的数量,或者您可以使用

SELECT * INTO #tempTable FROM ( <Your Query> )

然后使用dyanmic sql根据一系列IF条件确定最终列列表

DECLARE @colList VARCHAR(1000) = ''

-- If All values in col1 are NULL then COUNT(col1) gives zero    
IF (SELECT COUNT(col1) FROM #tempTable) > 0
   SET @colList = 'col1,'

IF (SELECT COUNT(col2) FROM #tempTable) > 0
   SET @colList = @colList + 'col2,'
.
.
.

IF (SELECT COUNT(colN) FROM #tempTable) > 0
   SET @colList = @colList + 'colN,'

IF @colList<>''
BEGIN
  --Remove last comma from column list
  SELECT @colList = LEFT(@colList,LEN(@colList) -1 )

  --EXEC SELECT query on generated colum list excluding columns which have ALL NULL values
  EXEC('SELECT ' + @colList + ' FROM #tempTable')

END