I have a table where I want to select the last 10% of rows, offset by 10% (so I want to select the last 80-90% of the data).
I wrote the following query
SELECT TOP 10 PERCENT
[col1], [col2]
FROM [table]
ORDER BY [col1] DESC
OFFSET 10 ROWS
But I receive the following error:
Line 5: Incorrect syntax near 'OFFSET'.
What am I doing wrong? I am using Microsoft SQL Server 2012 which should be compatible with OFFSET
SQL Median. Median refers to the "middle" number in a series of numbers. When the total count is odd, this is pretty straightforward. Assuming there are n numbers, the median number would be the (n+1)/2-th largest number (or the (n+1)/2-th smallest number -- both are the same number).
To select results from the middle of a sorted list, use ORDER BY clause along with LIMIT.
Try something like this....
SELECT TOP (50) PERCENT *
FROM (
SELECT TOP (20) PERCENT
[col1]
,[col2]
FROM [table]
ORDER BY [col1] DESC
)T
ORDER BY [col1] ASC
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