SQL Server 2008 - How to show rows between 1001 to 2000 - sql-server

I am using SQL Server 2008. With help of TOP clause in SQL Server I have run following query to display the first 1000 rows from my table Department
Select TOP 1000 *
from Department;
Now I need to display the next 1000 data from the same table. Can anybody help me to find a solution for the this?

You can use the ROW_NUMBER function to get the number of the row, and then take the numbers from X to Y. Below query is assuming that you want the results ordered by ID:
WITH OrderedDepartments AS
(
SELECT ID, Name,
ROW_NUMBER() OVER (ORDER BY ID) AS RowNumber
FROM Department
)
SELECT ID, Name
FROM OrderedDepartments
WHERE RowNumber BETWEEN 1001 AND 2000;

you can try something like:
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY DEPARTMENT_NAME) AS rownum
FROM DEPARTMENT
) AS t
WHERE t.rownum BETWEEN 1001 AND 2001

Best way is use "FETCH (Transact-SQL)", more info and sample available in following link,
FETCH (Transact-SQL)

Related

SQL Server : return id column using max on different column

In my table I have the columns id, userId and points. As a result of my query I would like to have the id of the record that contains the highest points, per user.
In my experience (more with MySQL than SQL Server) I would use the following query to get this result:
SELECT id, userId, max(points)
FROM table
GROUP BY userId
But SQL Server does not allow this, because all columns in the select should also be in the GROUP BY or be an aggregate function.
This is a hypothetical situation. My actual situation is a lot more complicated!
Use ROW_NUMBER window function in SQL Server
Select * from
(
select Row_Number() over(partition by userId Order by points desc) Rn,*
From yourtable
) A
Where Rn = 1

How to convert mysql query into SQL Server query?

How to covert mysql query into mssql query?
SELECT name FROM user LIMIT 5, 10
I have known that mssql don't support 'limit'...
But I have to use limit!
How to covert mysql query into SQL Server query?
Try out this
SELECT * FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY name) as row FROM sys.databases
) a WHERE a.row > 5 and a.row <= 10
You can achieve your concept.
select * from
(select name , ROW_NUMBER() over(order by name) rn from user ) a
where rn > 5 and rn<= 15
There is no way to translate this, but there is a bit of workaround here on SO.
Check this out.

How to page a View

I have a view in my sql server 2005 db.
I'm using Pythons PyMsSQL to select rows form the view. I'm not necessarily selecting all the rows in the view with this select. For example, I might select based on name or date etc.
How can I page this view? That is, how can I select row 0 to 9, or rows 10 to 19 etc.
Thanks for your help,
Barry
One option is to use the ROW_NUMBER window function to
add a number you can select on
using an ORDER BY date, name or any other column that guarantees the order between subsequent runs.
SQL Statement
SELECT *
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY Date ) AS RowNum, *
FROM AView
) AS AView
WHERE RowNum BETWEEN 1 AND 9

Variation on Select top n

Is is possible to do a variation of select top n rows to select top n rows starting at a row other than 0.
My (mobile) app has limited resources and no server side caching available. The maximum rows returned is 100. I get the first 100 by select top 100. I would then like the user to be then able to request rows 101-200 and so on. The database data is static and the the re-query time negligible.
Platform SQL Server 2008
Here's an article which demonstrates such queries using the ROW_NUMBER function.
;With CTETable AS
(
SELECT ROW_NUMBER() OVER (ORDER BY Column_Name DESC) AS ROW_NUM, * FROM TABLENAME WHERE <CONDITION>
)
SELECT Column_List FROM CTETable WHERE ROWN_NUM BETWEEN <StartNum> AND <EndNum>
Use your [startNum] and [EndNum] to be any series you want maybe 123 - 147 ! This will work well !

What's the best method to do paging in my ASP page

What's the best method to do paging in my ASP page when displaying a list of items?
I knew that there is no equivalent to MySQL's LIMIT clause present in SQL Server, either 2000 or 2005.
How can I retrieve some particular records (Records 20 to 30) from the DB?
Do i need to fetch all the records from the database and do something in my ASP page to display the required records?
Whats the best method to do paging in my ASP page when displaying a list of items ?
I just want to add one more feature to Mr. Wheat's answer. Why not u are trying to use the Take () and Skip() feature of linq(obviously if u are using dotnet framework 3.5+)
It is indeed helpful while working with large datasets.
Have a look
Using Take and Skip method in LINQ queries
I knew that there is no MySQL LIMIT clause present in SQL server(both 2000 and 2005 should support).how can i retrieve some particular records (Record 20 -30) from DB ?
You can do this in SQLSERVER 2005+ by using ranking function Row_Number() among other alternatives. A sample example is included herewith
First I am creating a dummy table and inserting some 50 records
declare #tbl table(name varchar(50),age int)
;with num_cte as
( select 1 as rn
union all
select rn+1 from num_cte where rn<50
)
insert #tbl
select names ,rn + 20 ageval
from num_cte
cross apply( select 'name' + CAST(rn as varchar(2)) AS names) names
select * from #tbl
Now by using the Row_Number() function I am picking up records between 20 & 30
select name,age from(
select ROW_NUMBER()over (order by age) as rownum,name,age from #tbl) X
where X.rownum between 20 and 30
However, for achieving the same in SQL SERVER 2000 the below query will help
select name,age from(
select t1.name,t1.age,
(select count(*)+1 from #tbl where name<>t1.name and age<=t1.age) rownum
from #tbl t1
)X(name,age,rownum)
where rownum between 20 and 30
Please see:
Paging In ASP.NET
Data Access Tutorials
GridView Examples for ASP.NET 2.0: Paging and Sorting the GridView's Data
SQL Server 2005 Paging – The Holy Grail
Youd need to use ROW_NUMBER (SQL Server 2005+)
SELECT * FROM
(SELECT a.*, ROW_NUMBER() OVER (ORDER BY hire_date) rn
FROM hr.employees AS OF TIMESTAMP (TIMESTAMP '2009-01-29 10:30:00') a)
WHERE rn BETWEEN 10 AND 19
Related answer
Using ROW_NUMBER, you are numering and sorting the inherently unsorted group (the table). Once you have an ordered set instead of just a set, you can now the sentence "I want all the rows from 10 to 19" makes sense.
You will have to use ASP code to keep both the upper and lower elements, so you can ask for the next or previous subset of rows to show.

Resources