假设我有一个像
这样的表Room Day People Theme
A 14/05/2014 12 Water
A 12/05/2014 245 Mathematics
A 05/04/2014 215 Nature
B 10/09/2013 252 Water
B 10/05/2012 221 Cinema
B 05/10/2011 215 Cinema
C 10/10/2013 224 Mathematics
C 02/06/2013 245 Cooking
C 05/03/2013 15 Cooking
并希望获得一个表格,其中包含每个房间A,B和C的行,以及说明其使用日期,参加人数和会议主题的列。
结果将是一个表格:
Room Day People Theme
A 14/05/2014 12 Water
B 10/09/2013 252 Water
C 10/10/2013 224 Mathematics
这只是一个例子,真实的表包含很多行。
是否有快速SQL代码可以在不说明所有房间的情况下获取该代码?
答案 0 :(得分:1)
您可以尝试此查询:
SELECT ROOM, MAX(DAY), PEOPLE, THEME
FROM TABLE_NAME
GROUP BY ROOM, PEOPLE, THEME;
答案 1 :(得分:0)
获取每间客房的最长日期。然后加入你的表格以获得完整的记录:
select rooms.*
from rooms
join
(
select room, max(day) as max_day
from rooms
group by room
) last_used on last_used.room = rooms.room and last_used.max_day = room.day;
答案 2 :(得分:0)
With CTE As (
Select room,
day,
people,
theme,
Row_Number() Over(Partition By Room Order By Day Desc) RowNumber
)
Select * From CTE Where RowNumber = 1