SQL Server Join with Latest 2 Entries

守給你的承諾、 提交于 2019-12-02 09:49:49

问题


I know the title of the post is bad but hear me out. A question like this arose the other day at work, and while I found a way around it, the problem still haunts me.

Lets assume Stackoverflow has only 3 tables.

Users ( username )
Comments ( comment, creationdate )
UsersCommentsJoin , this is the join table between the first 2 tables.

Now lets say I want to make a query that would return the all the users with the last 2 most recent comments. So the result set would look like this.

|username| most recent comment | second most recent comment|

How on earth do I go about creating that query ? I solved this problem earlier by simply only returning the most recent comment and not even trying to get the second one, and boy, let me tell you it seemed a WHOLE lot more involved than when I thought with subselects, TOP and other weird DB acrobatics.

Bonus Round Why do some queries which seem easy logically, turn out to be monster queries, at least from my rookie perspective ?

EDIT: I was using an MS SQL server.


回答1:


You can use a crosstab query pivoting on ROW_NUMBER

WITH UC 
     AS (SELECT UCJ.userId, 
                C.comment, 
                ROW_NUMBER() OVER (PARTITION BY userId 
                                       ORDER BY creationdate DESC) RN 
         FROM   UsersCommentsJoin UCJ 
                JOIN Comments C 
                  ON C.commentId = U.commentId) 
SELECT username, 
       MAX(CASE 
             WHEN RN = 1 THEN comment 
           END) AS MostRecent, 
       MAX(CASE 
             WHEN RN = 2 THEN comment 
           END) AS SecondMostRecent 
FROM   Users U 
       JOIN UC 
         ON UC.userId = U.userId 
WHERE  UC.RN <= 2 
GROUP  BY UC.userId 


来源:https://stackoverflow.com/questions/9243368/sql-server-join-with-latest-2-entries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!