Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select all rows except top row [duplicate]

how do I return all rows from a table except the first row. Here is my sql statement:

Select Top(@TopWhat) * 
from tbl_SongsPlayed 
where Station = @Station 
order by DateTimePlayed DESC

How do I alter my SQL statement to return all rows except the first row.

Many thanks

like image 787
D-Dawgg Avatar asked Feb 22 '13 20:02

D-Dawgg


People also ask

How do I SELECT all rows to except the first row in SQL?

The SQL EXCEPT operator is used to return all rows in the first SELECT statement that are not returned by the second SELECT statement. Each SELECT statement will define a dataset. The EXCEPT operator will retrieve all records from the first dataset and then remove from the results all records from the second dataset.

How do I exclude duplicate rows in SQL?

The SQL DISTINCT keyword, which we have already discussed is used in conjunction with the SELECT statement to eliminate all the duplicate records and by fetching only the unique records.

How do I prevent duplicate rows?

The go to solution for removing duplicate rows from your result sets is to include the distinct keyword in your select statement. It tells the query engine to remove duplicates to produce a result set in which every row is unique.

How do I exclude a duplicate column in SQL?

To remove the duplicate columns we use the DISTINCT operator in the SELECT statement as follows: Syntax: SELECT DISTINCT column1, column2, ...


3 Answers

SQL 2012 also has the rather handy OFFSET clause:

Select Top(@TopWhat) *
from tbl_SongsPlayed 
where Station = @Station 
order by DateTimePlayed DESC
OFFSET 1 ROWS
like image 168
chrisb Avatar answered Sep 18 '22 15:09

chrisb


Depending on your database product, you can use 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
like image 36
Taryn Avatar answered Sep 20 '22 15:09

Taryn


already 'Chrisb' has given a very neat answer. But you can also try this one...

The EXCEPT operand (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' was another clause that can be used.

like image 35
Som Poddar Avatar answered Sep 17 '22 15:09

Som Poddar