sql选择3列并在两列上进行重复数据删除

时间:2012-07-09 19:45:18

标签: sql sql-server sql-server-2008 duplicate-removal

我有一个作业设置,当前从一个不包含唯一索引的表中选择记录。我意识到这可以通过在表和相关列上放置一个索引来解决,但是,在这种情况下出于测试目的,我需要删除索引,然后执行select也将删除基于2列的重复项:

SELECT DISTINCT [author], [pubDate], [dateadded]
FROM [Feeds].[dbo].[socialPosts]
WHERE CAST(FLOOR(CAST(dateadded AS float)) AS datetime) > 
                               DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE() - 2), 0)  
AND CAST(FLOOR(CAST(dateadded AS float)) AS datetime) < 
                               DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE()), 0)

这将选择前一天的所有记录,我想根据作者和pubdate重复记录记录。这可以是一个事后选择或事先完成,但想法是找出它是否可以在一个选择中完成。

1 个答案:

答案 0 :(得分:1)

您可以在GROUP BY列上使用dateadded和任何汇总函数来获得唯一的author, pubdate结果。

SELECT  [author]
        ,[pubDate]
        ,MAX([dateadded])
 FROM   [Feeds].[dbo].[socialPosts]
 WHERE  CAST(FLOOR(CAST(dateadded AS float)) AS datetime) >  dateadd(day,datediff(day, 0, getdate()-2), 0)  
        AND CAST(FLOOR(CAST(dateadded AS float)) AS datetime) < dateadd(day,datediff(day, 0, getDate()), 0)
 GROUP BY 
        [author]
        , [pubdate]