How to use limit in Microsoft SQL Server 2008? - sql-server-2008

We have to use Limit keyword in SQL server 2008/2012.
We need to apply limit for every query where start index will change every time. When I was googling found TOP but it won't work for us. Can anyone please share how to use LIMIT keyword in sql server where every time start index change.
We need query in SQL server like below -
SELECT * from STOCK LIMIT 11, 10000 (where 11=start index, 10000=size)

This is probably not a good long-term solution for you, but if the table has an Identity field you could do something like this:
SELECT TOP 1000 * FROM STOCK
WHERE Id > 11
ORDER BY Id

You could use a CTE with a window function, like in the answer to this SO question -> Skip first row in SQL Server 2005?

You would have to use something like the ROW_NUMBER() function and then specify what you want from there.
select top 10000 *, row_number() over (order by [YourFieldName]) as row from Stock where row > 11

We have upgraded to SQL server 2012 and replaced query with OFFSET and FETCH. Sample is below.
SELECT ITEM_ID, PRICE FROM MENU ORDER BY ITEM_ID ASC OFFSET 10 ROWS FETCH NEXT 10 ROWS ONLY;

Related

Passing value to LIMIT function using SELECT query

There is a table named STATION.
I want to display half rows of the table.
MYSQL query
SELECT *
FROM STATION
LIMIT (SELECT COUNT(ID) FROM STATION)/2
I tried to perform a query like this but I am getting syntax error.
What is wrong in this query?
How can I perform this query?
One method is to use window functions:
select t.*
from (select t.*,
ntile(2) over (order by id) as tile
from t
) t
where tile = 1;
I have never seen a need of querying exactly half the table.
If you are asking this out of curiosity, that's fair but if there is really a need where you are trying to implement something like this, please revisit the design.
Coming to your question, you can possibly do two things:
Implement a stored procedure and query the count and store in a variable and then pass it on to the actual SELECT query for the LIMIT clause.
From your client code fire 2 different queries - 1 for count and calculate half (non fraction) and then pass it to the limit clause of next select query .

Fetch top record from query result for all databases

I have the following code.
What i need to do is when i fetch the records from the database, i need only the top record for my next procedure.
I have tried using LIMIT, TOP etc but that is not compatible with all databases.
Any help in this case is appreciated.
Below is the query
SELECT a,b,c,d,COUNT(*) AS cnt_next
FROM table
WHERE emp_cd='ASDF1234' and a < '01/08/2017'
GROUP BY a,b,c,d
ORDER BY b
DESC
In sql server 2012 you use "OFFSET 1 ROWS FETCH NEXT 1 ROWS ONLY" . Not sure if this will help but worth a shot!
There are many differences between RDBMS, so it's impossible for one query that be compatible with all databases.
If you're using Mysql and Postgresql, this query could work on these 2 RDBMS.
SELECT *
FROM
(SELECT a,b,c,d,COUNT(*) AS cnt_next
FROM table
WHERE emp_cd='ASDF1234' and a < '01/08/2017'
GROUP BY a,b,c,d
ORDER BY b DESC) data
LIMIT 1
For other RDBMSs, you could find more information at this link

SQL - How can I find the average of an SQL statement with a LIMIT?

Currently I'm working with a database on phpmyadmin. I'm trying to find the Average of an SQL statement that is implementing a LIMIT code.
SQL Statement -
SELECT avg(value) FROM que LIMIT 10
The problem with the code is its not averaging the first 10 numbers in value column, but all of them. So the LIMIT 10 isn't actually working. Is there anyway to avoid this or an alternative?
You need to use a subquery:
SELECT avg(value)
FROM (select value
from que
LIMIT 10
) q;
Do note, however, that use of limit without an order by produces arbitrary results -- there is no definition of the "first ten" records in a table.

MySQL LIMIT clause equivalent for SQL SERVER

I've been doing a lot of reading on alternatives to the LIMIT clause for SQL SERVER. It's so frustrating that they still refuse to adapt it. Anyway, I really havn't been able to get my head around this. The query I'm trying to convert is this...
SELECT ID, Name, Price, Image FROM Products ORDER BY ID ASC LIMIT $start_from, $items_on_page
Any assistance would be much appreciated, thank you.
In SQL Server 2012, there is support for the ANSI standard OFFSET / FETCH syntax. I blogged about this and here is the official doc (this is an extension to ORDER BY). Your syntax converted for SQL Server 2012 would be:
SELECT ID, Name, Price, Image
FROM Products
ORDER BY ID ASC
OFFSET (#start_from - 1) ROWS -- not sure if you need -1
-- because I don't know how you calculated #start_from
FETCH NEXT #items_on_page ROWS ONLY;
Prior to that, you need to use various workarounds, including the ROW_NUMBER() method. See this article and the follow-on discussion. If you are not on SQL Server 2012, you can't use standard syntax or MySQL's non-standard LIMIT but you can use a more verbose solution such as:
;WITH o AS
(
SELECT TOP ((#start_from - 1) + #items_on_page)
-- again, not sure if you need -1 because I
-- don't know how you calculated #start_from
RowNum = ROW_NUMBER() OVER (ORDER BY ID ASC)
/* , other columns */
FROM Products
)
SELECT
RowNum
/* , other columns */
FROM
o
WHERE
RowNum >= #start_from
ORDER BY
RowNum;
There are many other ways to skin this cat, this is unlikely to be the most efficient but syntax-wise is probably simplest. I suggest reviewing the links I posted as well as the duplicate suggestions noted in the comments to the question.
For SQL Server 2005 and 2008
This is an example query to select rows from 11 to 20 from Report table ordered by LastName.
SELECT a.* FROM
(SELECT *, ROW_NUMBER() OVER (ORDER BY LastName) as row FROM Report) a
WHERE a.row > 10 and a.row <= 20
Try this:
SELECT TOP $items_on_page ID, Name, Price, Image
FROM (SELECT TOP $start_from + $items_on_page - 1 * FROM Products ORDER BY ID) as T
ORDER BY ID DESC
EDIT: Explanation-
No getting around the subquery, but this is an elegant solution.
Say you wanted 10 items per page, starting on the 5th row, this would give you the bottom 10 rows of the top 14 rows. Essentially LIMIT 5,10
you can use ROW COUNT : Returns the number of rows affected by the last statement. when you don, you reset the rowcont.
SET ROWCOUNT 100
or
you can try using TOP query
SELECT TOP 100 * FROM Sometable ORDER BY somecolumn
If you allow the application to store a tad of state, you can do this using just TOP items_on_page. What you do is to store the ID of the last item you retrieved, and for each subsequent query add AND ID > [last ID]. So you get the items in batches of items_on_page, starting where you left off each time.

Order of execution of ORDER BY and LIMIT in a MySQL query

I have a query like this where I want to display sorted records in a paginated format.
This is my query.
SELECT * FROM answers
WHERE userid = '10'
ORDER BY votes LIMIT 10, 20
The two arguments to LIMIT will be used for generating page-by-page display of records. Now, my problem is ORDER BY. How does MySQL execute this query?
1st way
Select the records according to filters
Sort them
Limit them
2nd way
Select the records according to filters
Limit them
Sort them
If I think like MySQL engine, I would want to implement 2nd, since I would then have to sort lesser records?
Can somebody throw some light on this?
Regards
A SQL LIMIT will in all databases always work on the result of a query, so it will run the query with the ORDER BY and that result will then be limited. This is a functional requirement, the database not perse needs to execute it like that.
Thus the 1st way.
Mysql follows the 1st way.
If you want your 2nd way, try using a subquery with the limit, then order.
Something like:
SELECT * FROM
(
SELECT * FROM answers
WHERE userid = '10'
LIMIT 10
)
ORDER BY votes
The 1st way as mentioned by the last two answers. From a user perspective, the 2nd way won't be very useful anyways.