SQL Server使用CROSS APPLY来避免SELECT语句中的重复记录

时间:2015-09-17 05:35:30

标签: sql sql-server

我有一张表没有主键或任何唯一列(没有任何自动增量列)。

我必须从该表中选择记录以避免重复记录。我已经使用了ROW_NUMBER()功能,它为我工作。

这是工作代码

     WITH Result AS
        (
        SELECT ISNULL(ROW_NUMBER() OVER (ORDER BY 
                                          CategoryName
                                         ,ProductGroup
                                         ,Brand
                                         ,Division 
                                         ,ProductNo                                                                                                                     
                                         ,LocationNo                                                                                         
                                         ,Season                                                                                                
                                         ,Year                                                                                          
                                         ,Month                                                                                         
                                         ,Week                                                                                          
                                         ,Day                                                                                           
                                         ,DemandQty
                                      ), 0) AS RN, * 
        FROM TABLE_1
   )
SELECT * 
FROM Result AS D

WHERE RN IN
 (
    SELECT MAX(RN) MAX_RN
    FROM Result 
    GROUP BY
   CategoryName
  ,ProductGroup
  ,Brand
  ,Division 
  ,ProductNo                                                                                                                    
  ,LocationNo                                                                                        
  ,Season                                                                                               
  ,Year                                                                                         
  ,Month                                                                                            
  ,Week                                                                                         
  ,Day                                                                                          
  ,DemandQty
)   

这样可以避免重复记录。

我需要做的是没有编写这个WITH条款我需要使用CROSS APPLY来编写它,因为当 500000时,选择记录需要花费太多时间行号

的记录

到目前为止,我尝试重新编写查询,如下所示,以避免重复记录。

SELECT * FROM 
TABLE_1 d
CROSS APPLY (
SELECT TOP 1 * FROM
TABLE_1 x
WHERE x.[Season] = d.Season
AND ISNULL(x.[Brand]            ,'')= ISNULL(d.[Brand]            ,'')
AND ISNULL(x.[Division]         ,'')= ISNULL(d.[Division]         ,'')
AND ISNULL(x.[CategoryName]     ,'')= ISNULL(d.[CategoryName]     ,'')
AND ISNULL(x.[ProductGroup]     ,'')= ISNULL(d.[ProductGroup]     ,'')
AND ISNULL(x.[ProductNo]        ,'')= ISNULL(d.[ProductNo]        ,'')
AND ISNULL(x.[LocationNo]       ,'')= ISNULL(d.[LocationNo]       ,'')
AND ISNULL(x.[Year]             ,'')= ISNULL(d.[Year]             ,'')
AND ISNULL(x.[Month]            ,'')= ISNULL(d.[Month]            ,'')
AND ISNULL(x.[Week]             ,'')= ISNULL(d.[Week]             ,'')
AND ISNULL(x.[Day]              ,'')= ISNULL(d.[Day]              ,'')
AND ISNULL(x.[DemandQty]        ,'')= ISNULL(d.[DemandQty]        ,'')
AND ISNULL(x.[DeletionIndicator],'')= ISNULL(d.[DeletionIndicator],'')
AND ISNULL(x.[CreatedBy]        ,'')= ISNULL(d.[CreatedBy]        ,'')
AND ISNULL(x.[CreatedOn]        ,'')= ISNULL(d.[CreatedOn]        ,'')
AND ISNULL(x.[UpdatedBy]        ,'')= ISNULL(d.[UpdatedBy]        ,'')
AND ISNULL(x.[UpdatedOn]        ,'')= ISNULL(d.[UpdatedOn]        ,'')
) y

但它没有给我正确的结果仍然会有重复的记录?

假设有10个相同的行以及其他行,我需要从这10个相同的记录中检索一条记录以及其他不重复的行。

有没有办法在不使用ROW_NUMBERWITH的情况下执行此操作?

1 个答案:

答案 0 :(得分:1)

为避免重复,您可以使用DISTINCT

SELECT DISTINCT 
      CategoryName
     ,ProductGroup
     ,Brand
     ,Division 
     ,ProductNo         
     ,LocationNo                 
     ,Season                        
     ,Year                  
     ,Month                 
     ,Week                  
     ,Day                   
    ,DemandQty
FROM TABLE_1;

GROUP BY

 SELECT
      CategoryName
     ,ProductGroup
     ,Brand
     ,Division 
     ,ProductNo         
     ,LocationNo                 
     ,Season                        
     ,Year                  
     ,Month                 
     ,Week                  
     ,Day                   
    ,DemandQty
FROM TABLE_1
GROUP BY
      CategoryName
     ,ProductGroup
     ,Brand
     ,Division 
     ,ProductNo         
     ,LocationNo                 
     ,Season                        
     ,Year                  
     ,Month                 
     ,Week                  
     ,Day                   
     ,DemandQty;

用于比较可以包含NULL而不是

的值 你可以使用

AND ISNULL(x.[Brand],'')= ISNULL(d.[Brand],'')

WHERE EXISTS (
  SELECT
      x.CategoryName
     ,x.ProductGroup
     ,x.Brand
     ,x.Division 
     ,x.ProductNo         
     ,x.LocationNo                 
     ,x.Season                        
     ,x.Year                  
     ,x.Month                 
     ,x.Week                  
     ,x.Day                   
     ,x.DemandQty
  INTERSECT
  SELECT
      d.CategoryName
     ,d.ProductGroup
     ,d.Brand
     ,d.Division 
     ,d.ProductNo         
     ,d.LocationNo                 
     ,d.Season                        
     ,d.Year                  
     ,d.Month                 
     ,d.Week                  
     ,d.Day                   
     ,d.DemandQty
)