【问题标题】:Removing duplicate records in sql view删除sql视图中的重复记录
【发布时间】:2012-12-07 22:54:51
【问题描述】:

我有一个查询从表中返回数据,该表的第 1 列有重复记录,但其他列中可能有不同的值。我只想将第 1 列中的每个值的一条记录带入使用标准选择正确记录的视图中。

这里是查询;

SELECT 
   PrimaryColumn1,
   Column2,
   Column3,
   Date1,
   Date2
FROM
   My_Table

我希望在我根据Date1 中的最新日期创建的视图中只有 PrimaryColumn1 中的不同值,如果这也是重复的,则在 Date2 中。

我尝试执行以下操作,但无法使其工作

SELECT 
   PrimaryColumn1,
   Column2,
   Column3,
   Date1,
   Date2
FROM    
   (SELECT  
        [PrimaryColumn1,
        Column2,
        Column3,
        Date1,
        Date2,
        ROW_NUMBER() OVER(PARTITION BY [Date1] ORDER BY Date2) AS RowNumber
    FROM    
        My_Table)
WHERE   
    RowNumber = 1

任何帮助将不胜感激。

根据以下建议,最终查询如下所示:

SELECT 
    PrimaryColumn1,
    Column2, 
    Column3,
    Date1,
    Date2
FROM    
    (SELECT  
         [PrimaryColumn1,
         Column2,
         Column3,
         Date1,
         Date2,
         ROW_NUMBER() OVER(PARTITION BY PrimaryColumn1
                           ORDER BY Date1 DESC, Date2 DESC) AS RowNumber) data
WHERE 
    RowNumber = 1

【问题讨论】:

    标签: sql view duplicates


    【解决方案1】:

    我认为你的ROW_NUMBER() 声明应该是这样的:

    ROW_NUMBER() OVER(PARTITION BY PrimaryColumn1
                          ORDER BY Date1 DESC, Date2 DESC) AS RowNumber
    

    由于您正在寻找每个 PrimaryColumn1 值的最新记录,这应该可以满足您的需求(据我所知)。

    【讨论】:

      【解决方案2】:

      CROSS APPLY 是执行此类操作的好方法。例如,这会为 Products 表中的每个 CategoryID 提取一条记录,并显示每个类别中最昂贵产品的产品数据。

      这有效地为您提供了一种在连接中使用相关子查询的方法。很酷。

      USE Northwind;
      GO
      --Get a distinct list of CategoryID values
      --From the dbo.Products table, and show the
      --Product details for the most expensive product
      --in each of those categories....
      SELECT DISTINCT 
        Prod.CategoryID,
        TopProd.ProductID,
        TopProd.ProductName,
        TopProd.UnitPrice
      FROM dbo.Products AS Prod
      CROSS APPLY 
      (
        --Find the top 1 product in each category by unitprice
        SELECT TOP 1 
          ProductID,
          ProductName,
          UnitPrice
        FROM dbo.Products
        --This is the "correlated" part where 
        --we filter by the outer queries' CategoryID
        WHERE CategoryID = Prod.CategoryID
        --The ORDER BY determines which product
        --you see for each category.  In this
        --case I'll get the most expensive product
        ORDER BY UnitPrice DESC
      ) AS TopProd;
      

      【讨论】:

        【解决方案3】:
        SELECT 
           PrimaryColumn1,
           Column2,
           Column3,
           Date1,
           Date2
        FROM My_Table
        INNER JOIN  
           (SELECT  PrimaryColumn1,
                MAX(Date1) AS max_Date1,
                MAX(Date2) AS max_Date2,
                FROM My_Table
                GROUP BY PrimaryColumn1
            ) AS Maxes
        ON Maxes.PrimaryColumn1 = My_Table.PrimaryColumn1
        AND Maxes.max_Date1 = My_Table.Date1
        AND Maxes.max_Date2 = My_Table.Date2
        

        【讨论】:

          猜你喜欢
          • 2020-07-16
          • 2021-02-13
          • 2016-01-07
          • 2010-10-24
          • 1970-01-01
          • 1970-01-01
          • 2011-03-20
          • 1970-01-01
          相关资源
          最近更新 更多