0

由于我不擅长 sql 和设计器无法使用 OVER ,因此为此苦苦挣扎。基本上,如果用户关注关联的标签,这将获取主题列表。

我需要分组T.TopicId以停止重复。如果用户选择与某个主题相关的多个标签,它将列出该主题两次(每个标签一次)

当我在 sql 中添加 group by 时,出现多个错误,并且我尝试重新安排事物并且无法使其正常工作,正如所说的,我对 sql 语句没用

@id int = null
AS
SELECT
*
FROM
(SELECT
    ROW_NUMBER()
        OVER 
            (ORDER BY TopicOrder desc
             ,
             (CASE 
                WHEN M.MessageCreationDate > T.TopicCreationDate THEN M.MessageCreationDate
                ELSE T.TopicCreationDate
            END) desc)
        AS RowNumber
    ,T.TopicId, T.TopicTitle, T.TopicShortName, T.TopicDescription, T.TopicCreationDate, T.TopicViews, T.TopicReplies, T.UserId, T.TopicTags, T.TopicIsClose, 
                     T.TopicOrder, T.LastMessageId, T.UserName, M.MessageCreationDate, M.UserId     AS MessageUserId, MU.UserName AS MessageUserName, U.UserGroupId, 
                     U.UserPhoto, T.UserFullName
FROM            Tags INNER JOIN
                     TopicsComplete AS T ON T.TopicId = Tags.TopicId LEFT OUTER JOIN
                     Messages AS M ON M.TopicId = T.TopicId AND M.MessageId = T.LastMessageId AND     M.Active = 1 LEFT OUTER JOIN
                     Users AS MU ON MU.UserId = M.UserId LEFT OUTER JOIN
                     Users AS U ON U.UserId = T.UserId LEFT OUTER JOIN
                     tagfollows AS TF ON @id = TF.userid
WHERE        (Tags.Tag = TF.tag)
 )T 

如果有人可以提供帮助,将不胜感激,谢谢!:)

4

2 回答 2

0

您说您想在用户关注的集合中显示带有标签的帖子,但您不希望帖子在具有多个匹配标签时多次显示。EXISTS这是子查询的完美用途。这是该 MSDN 页面中的一个示例。

SELECT a.FirstName, a.LastName
FROM Person.Person AS a
WHERE EXISTS
(SELECT * 
    FROM HumanResources.Employee AS b
    WHERE a.BusinessEntityID = b.BusinessEntityID
    AND a.LastName = 'Johnson');

您对 person 表(例如您的 posts 表)非常感兴趣,但您希望显示在 employee 中至少有一条匹配记录的记录(例如您的 tags 表)。

于 2013-03-23T16:13:01.597 回答
0

我认为您只需要将联接转换tagfollowsEXISTS子查询(并删除多余的嵌套):

SELECT
    ROW_NUMBER()
        OVER ( ORDER BY TopicOrder desc
                      , CASE WHEN M.MessageCreationDate > T.TopicCreationDate 
                            THEN M.MessageCreationDate
                            ELSE T.TopicCreationDate
                        END desc )
        AS RowNumber,
    T.TopicId, T.TopicTitle, T.TopicShortName, T.TopicDescription, 
    T.TopicCreationDate, T.TopicViews, T.TopicReplies, T.UserId, 
    T.TopicTags, T.TopicIsClose, T.TopicOrder, T.LastMessageId, 
    T.UserName, M.MessageCreationDate, 
    M.UserId    AS MessageUserId, 
    MU.UserName AS MessageUserName, 
    U.UserGroupId, U.UserPhoto, T.UserFullName
FROM
        TopicsComplete AS T
    LEFT OUTER JOIN
        Messages AS M        ON  M.TopicId = T.TopicId 
                             AND M.MessageId = T.LastMessageId
                             AND M.Active = 1 
    LEFT OUTER JOIN
        Users AS MU          ON  MU.UserId = M.UserId 
    LEFT OUTER JOIN
        Users AS U           ON  U.UserId = T.UserId
WHERE   EXISTS
        ( SELECT  *
          FROM    Tags
              INNER JOIN tagfollows AS TF
                  ON  Tags.Tag = TF.tag 
          WHERE   T.TopicId = Tags.TopicId 
            AND   @id = TF.userid   
        ) ;
于 2013-03-23T16:17:33.763 回答