0

我正在使用 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
4

2 回答 2

1

如果您需要 A 中的所有列,包括 Id,我认为您将很难包括 Id。我认为您需要明确列出您所追求的 A 中的列。我还假设您想要对要分组的记录进行计数,因此需要 Count(TheDate) 元素。

除此之外,请查看仅获取日期时间和组的日期部分。

就像是;

SELECT ActionType, UserId, SmoothieId, SmoothieAuthorId,
       TheDate, Count(TheDate) AS Occurances, UserName, SmoothieAuthorName, 
       SmoothieName, SmoothieId
  FROM (
    SELECT  TOP 25 A.ActionType, A.UserId, A.SmoothieId,  
            A.SmoothieAuthorId, 
            DATEADD(dd, 0, DATEDIFF(dd, 0, CreatedDate)) AS TheDate, 
            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
) x GROUP BY ActionType, UserId, SmoothieId, SmoothieAuthorId, UserName, 
             TheDate, SmoothieAuthorName, SmoothieName, SmoothieId
    ORDER BY The Date DESC

注意这未经测试,只是我尝试的快速建议。

于 2013-01-17T04:57:39.997 回答
0

我不确定我是否完全理解这个问题。假设您想要这些列的不同值,那么只需使用DISTINCT

SELECT DISTINCT 
    CAST(CreatedDate to Date) as DateWanted, 
    SmoothieId  
FROM 
(    
    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
) t 

查看您的评论,您说您需要活动表中的所有字段。对于前 2 条记录(1 或 2),您希望在 Id 列中收到什么?其他列中的所有其他值是否相同?要仅获取一条记录,您将需要选择一行而不是另一行,或者对具有相同信息的列进行分组。

祝你好运。

于 2013-01-17T04:34:04.287 回答