我正在使用 sql server 2008 r2。这是我的查询。
SELECT TOP 25 A.*, U.Displayname AS UserName,
SU.Displayname AS SmoothieAuthorName,
S.Name AS SmoothieName, S.Id AS SmoothieId
FROM dbo.Activity AS A
LEFT JOIN dbo.[User] AS U ON A.UserId = U.Id
LEFT JOIN dbo.[User] AS SU ON A.SmoothieAuthorId = SU.Id
LEFT JOIN dbo.Smoothie AS S ON A.SmoothieId = S.Id
WHERE A.UserId = 2 --@UserId
AND A.UserId <> A.SmoothieAuthorId
ORDER BY CreatedDate DESC
以下是我的结果。现在,我需要按 SmoothieId 和 CreatedDate 对它们进行分组(仅分组日期部分,忽略时间)。前两个应该只返回一个,3到5应该只返回一个。不知道怎么做,请帮忙。
Id ActionType UserId SmoothieId SmoothieAuthorId CreatedDate UserName SmoothieAuthorName SmoothieName SmoothieId
1 view 2 128 1 2013-01-15 20:05:03.403 mike test1234 new testing 2d 128
2 view 2 128 1 2013-01-15 20:16:24.733 mike test1234 new testing 2d 128
12 view 2 128 1 2013-01-16 21:45:56.167 mike test1234 new testing 2d 128
13 view 2 128 1 2013-01-16 22:12:51.217 mike test1234 new testing 2d 128
14 view 2 128 1 2013-01-16 22:12:54.407 mike test1234 new testing 2d 128
15 view 2 69 1 2013-01-16 22:19:54.783 mike test1234 sdfsdfwww 69