如何避免SQL查询的最终结果中的重复

时间:2018-02-17 11:21:18

标签: sql-server

DECLARE @Doc INT ='21'



SELECT DISTINCT 

       [Name or title]
      ,[Subject]
      ,[Approval Description]
      ,TypeOfApproval.[Name] AS [TypeOfApproval]
      ,TypeOfExpense.[Name] AS [TypeOfExpense]
      ,ExpenseNature.[Name] AS [ExpenseNature]
      ,BudgetType.[Name] AS [BudgetType]
      ,SN.[Name] AS [Current State] 
      ,[ImageBits] AS Imj
 
  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] SN  ON Rec.[State_ID] >= SN.ID
  
  LEFT JOIN  [SaveImage].[dbo].[Img] Imj  ON Imj.[CreatorName] = SN.[Name] 
  
  WHERE  Rec.[ID] = (@Doc)

我想消除除一列之外的同一对象的重复记录,因为该列中有不同的值,我需要获取该列的所有值而不重复。

以下是我现在获得的结果的屏幕截图:

enter image description here

这是我用来获取附加结果集的代码,如果我在状态表连接中将Arithmatic运算符更改为'=',它将只根据其当前状态获取一个结果。

2 个答案:

答案 0 :(得分:0)

; with cte as
(
select [approval description]
       , typeofapproval
       , typeofexpense
       , expensenature
       , budgettype
       , [current state]
       , imj
       , column_or_composition_of_columns_that_are_primary_key 
       , rn = row_number() over (partition by column_or_composition_of_columns_that_are_primary_key order by [current state])
from table_name
)
select cte.[approval description]
       , cte.typeofapproval
       , cte.typeofexpense
       , cte.expensenature
       , cte.budgettype
       , cte.currentstate
       , cte.imj
       , cte2.imj
       , cte3.imj
       , cte4.imj
from cte
where rn = 1
left join (select imj
        from cte
        where rn = 2) cte2 on cte.column_or_composition_of_columns_that_are_primary_key = cte2.column_or_composition_of_columns_that_are_primary_key
left join (select imj
        from cte
        where rn = 3) cte3   on cte.column_or_composition_of_columns_that_are_primary_key = cte3.column_or_composition_of_columns_that_are_primary_key
left join (select imj
        from cte
        where rn = 4) cte3   on cte.column_or_composition_of_columns_that_are_primary_key = cte4.column_or_composition_of_columns_that_are_primary_key

请注意,您需要在表格中按ID进行分区。也可以通过这一栏加入。

答案 1 :(得分:0)

您是否尝试过这个:使用DISTINCT ON(Column_Name)

SELECT DISTINCT ON Column_1, column_2 FROM TABLE_NAME