我有一个作业设置,当前从一个不包含唯一索引的表中选择记录。我意识到这可以通过在表和相关列上放置一个索引来解决,但是,在这种情况下出于测试目的,我需要删除索引,然后执行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重复记录记录。这可以是一个事后选择或事先完成,但想法是找出它是否可以在一个选择中完成。
答案 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]