T-SQL组跳过并取走

时间:2012-01-20 01:09:42

标签: sql-server tsql

考虑以下表格:

tables

如何从表中跳过并取组?尝试使用Row_Number(),但它没有帮助。有什么想法吗?

二手查询

;WITH cte AS (SELECT  Room.Id,  Room.RoomName,
                 ROW_NUMBER() OVER 
                 (ORDER BY Room.Id) AS RN
         FROM    Room INNER JOIN
                 RoomDetails ON Room.Id = RoomDetails.RoomId)
SELECT  Id, RoomName
FROM    cte 
WHERE RN = 1

1 个答案:

答案 0 :(得分:4)

您需要使用partition作为dense_rank函数的一部分

dense_rank() over (partition by roomid) as row

请点击此处查看更多示例Windowing functions