消除SQL Server视图中的重复ID

时间:2017-12-07 14:05:42

标签: sql sql-server view duplicates

我正在从不同的表创建此视图。但是看到一些IDS的问题是重复的。我想从中消除重复的ID。我可以这样做。我知道我必须使用Distinct关键字,但我怎么能在这里使用它?

 SELECT        dbo.Dispatch.Date, dbo.Dispatch.ID AS Dispatch_ID, dbo.Dispatch.Demand_ID AS Dispatch_DemandID, dbo.Dispatch.Full_Weight, dbo.Dispatch.Empty_Weight, dbo.Dispatch.Status, dbo.Dispatch.PDate, 
                         dbo.Customer.Name AS CustomerName, dbo.Demand.ID AS Demand_ID, dbo.Material.Bulk_Bags, dbo.Material.Type AS MaterialType, dbo.Material.Name AS MaterialName, 
                         dbo.[Order].Order_ID AS Order_Order_ID, dbo.[Order].Order_Date, dbo.Dispatch.PYear, dbo.Dispatch.PWeek, dbo.Dispatch.PMonth, dbo.[Order].Total_Quantity, dbo.[Order].RatePerTon, dbo.[Order].Discount, 
                         dbo.[Order].Final_Cost, dbo.[Order].Payment_Mode, dbo.[Order].Tax, dbo.[Order].Added_Value, dbo.Dispatch.Full_Weight - dbo.Dispatch.Empty_Weight AS DeliveredQuantity, dbo.Demand.Order_ID, 
                         dbo.Total_Amount_Status.Total_Amount, dbo.Total_Amount_Status.Amount_status
FROM                     dbo.Dispatch INNER JOIN
                         dbo.Demand ON dbo.Dispatch.Demand_ID = dbo.Demand.ID INNER JOIN
                         dbo.[Order] ON dbo.Demand.Order_ID = dbo.[Order].Order_ID INNER JOIN
                         dbo.Material ON dbo.[Order].Material_ID = dbo.Material.ID INNER JOIN
                         dbo.Customer ON dbo.[Order].Customer_ID = dbo.Customer.ID LEFT OUTER JOIN
                         dbo.Total_Amount_Status ON dbo.[Order].Order_ID = dbo.Total_Amount_Status.Order_ID
WHERE        (dbo.Dispatch.Status = 'EXIT') AND (dbo.Dispatch.Date = CONVERT(DATETIME, '2017-12-06 00:00:00', 102)) order by Dispatch.ID desc

2 个答案:

答案 0 :(得分:1)

您可以使用ROW_NUMBER() OVER(PARTITION BY XX ORDER BY XX) Function,然后选择返回1的行。

这应该通过使用主键来识别唯一的行来完成。

示例可能如下:

SQL代码

    with cte as ( SELECT        dbo.Dispatch.Date, dbo.Dispatch.ID AS Dispatch_ID, dbo.Dispatch.Demand_ID AS Dispatch_DemandID, dbo.Dispatch.Full_Weight, dbo.Dispatch.Empty_Weight, dbo.Dispatch.Status, dbo.Dispatch.PDate, 
                         dbo.Customer.Name AS CustomerName, dbo.Demand.ID AS Demand_ID, dbo.Material.Bulk_Bags, dbo.Material.Type AS MaterialType, dbo.Material.Name AS MaterialName, 
                         dbo.[Order].Order_ID AS Order_Order_ID, dbo.[Order].Order_Date, dbo.Dispatch.PYear, dbo.Dispatch.PWeek, dbo.Dispatch.PMonth, dbo.[Order].Total_Quantity, dbo.[Order].RatePerTon, dbo.[Order].Discount, 
                         dbo.[Order].Final_Cost, dbo.[Order].Payment_Mode, dbo.[Order].Tax, dbo.[Order].Added_Value, dbo.Dispatch.Full_Weight - dbo.Dispatch.Empty_Weight AS DeliveredQuantity, dbo.Demand.Order_ID, 
                         dbo.Total_Amount_Status.Total_Amount, dbo.Total_Amount_Status.Amount_status
                         ,ROW_NUMBER() OVER(PARTITION BY dbo.Dispatch.ID order by dbo.Dispatch.ID) as rn
FROM                     dbo.Dispatch INNER JOIN
                         dbo.Demand ON dbo.Dispatch.Demand_ID = dbo.Demand.ID INNER JOIN
                         dbo.[Order] ON dbo.Demand.Order_ID = dbo.[Order].Order_ID INNER JOIN
                         dbo.Material ON dbo.[Order].Material_ID = dbo.Material.ID INNER JOIN
                         dbo.Customer ON dbo.[Order].Customer_ID = dbo.Customer.ID LEFT OUTER JOIN
                         dbo.Total_Amount_Status ON dbo.[Order].Order_ID = dbo.Total_Amount_Status.Order_ID
WHERE        (dbo.Dispatch.Status = 'EXIT') AND (dbo.Dispatch.Date = CONVERT(DATETIME, '2017-12-06 00:00:00', 102)) 

)

select * from CTE
where rn = 1
order by Dispatch_ID desc

答案 1 :(得分:1)

只需在DISTINCT之后添加SELECT

您也可以在另一列中包含相同ID但不同数据的行。如果您不关心要显示的数据,可以使用:

SELECT TOP 1 WITH TIES
...
ORDER BY ROW_NUMBER() OVER (PARTITION BY dispatch_ID ORDER BY (SELECT NULL))

一个注意事项:请使用aliases