如何从第一行以外的表中返回所有行。这是我的sql语句:
Select Top(@TopWhat) *
from tbl_SongsPlayed
where Station = @Station
order by DateTimePlayed DESC
如何更改我的SQL语句以返回除第一行之外的所有行。
非常感谢
答案 0 :(得分:24)
SQL 2012还有一个非常方便的OFFSET子句:
Select Top(@TopWhat) *
from tbl_SongsPlayed
where Station = @Station
order by DateTimePlayed DESC
OFFSET 1 ROWS
答案 1 :(得分:5)
您可以使用row_number()
:
select *
from
(
Select s.*,
row_number() over(order by DateTimePlayed DESC) rn
from tbl_SongsPlayed s
where s.Station = @Station
) src
where rn >1
答案 2 :(得分:1)
已经'Chrisb'给出了一个非常巧妙的答案。但你也可以尝试这个...
EXCEPT操作数(http://msdn.microsoft.com/en-us/library/ms188055.aspx)
Select Top(@TopWhat) *
from tbl_SongsPlayed
Except Select Top(1) *
from tbl_SongsPlayed
where Station = @Station
order by DateTimePlayed DESC
'Not In'是另一个可以使用的条款。
答案 3 :(得分:0)
假设您拥有tbl_SongsPlayed
的唯一ID,则可以执行以下操作:
// Filter the songs first
With SongsForStation
As (
Select *
From tbl_SongsPlayed
Where Station = @Station
)
// Get the songs
Select *
From SongsForStation
Where SongPlayId <> (
// Get the top song, most recently played, so you can exclude it.
Select Top 1 SongPlayId
From SongsForStation
Order By DateTimePlayed Desc
)
// Sort the rest of the songs.
Order By
DateTimePlayed desc
Where