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
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.
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.
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.
To remove the duplicate columns we use the DISTINCT operator in the SELECT statement as follows: Syntax: SELECT DISTINCT column1, column2, ...
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
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With