Select middle rows in sql - sql

I have a table in my SQLServer database with almost 100K records in it and a web application in which I want to show a paged gridView presentation of these rows. Clearly I should filter the rows and return a small subset of them to client(because of the Ajax performance on web).
Here's my main problem. What's the best approach to select the middle rows? For example How can I select the rows from #50000 to #50010? Is there a way like select top 10 or select bottom 10 that selects the rows from middle of the table rows.
I'm using linq2sql in a .NET MVC web application & also can code SQL StoredProcedures.
Any suggestion will be appreciated.

Not sure abt this, but anyway
SELECT *
FROM (SELECT ROW_NUMBER() OVER(ORDER BY colm) RowNumr, colm FROM table) t
WHERE RowNumr BETWEEN 50000 AND 50010

In Linq2Sql this is not that hard. You can use:
dataContext.GetTable()
.Skip(50000)
.Take(10)
.ToList();
Scott Guthrie has a post on this matter, which has some more explanation. And Linq2Sql will actually produce sql using ROW_NUMBER().
Some small hint, the order of your expressions is important:
.Where()
.Select()
.OrderBy()
.Skip()
.Take()

http://msdn.microsoft.com/en-us/library/ms186734.aspx
Check this link out, you can use the rownumber to get the rows you need based on a sort order. Just add a where clause where the rownumber is between the your paging limits.

Try this:
select *
from(select table_name.*,rownum as rn from table_name order by column desc) table_name
where rn between 5000 and 5010;

Related

Get specified quantity of entities from table sorted by timestamp

It's complicated but I will try to explain what do I mean. My idea is to server send to client next 50 records while user is scrolling a page. When user will arrive to specified in frontend point, fontend will ask server for next 50 entities. What SQL query do I need?
A query should work like (in pseudo-SQL): SORT TABLE table_name SELECT entity BY INDEX (150-200).
I hope that I've explained understandable.
Thanks so much ;)
EDIT 1
I'm using PostgreSQL.
I think the answer will depend on the SQL dialect that you are using. Below is a small example using T-SQL. It should point you in the right direction.
Here you should find an answer to your problem in T-SQL dialect:
FETCH and OFFSET explained
Edit:
For PostgreSQL check the following link:
PostgreSQL FETCH example
SELECT
product_name,
list_price
FROM
production.products
ORDER BY
list_price,
product_name
OFFSET 10 ROWS
FETCH NEXT 10 ROWS ONLY;
If you are running the latest version of Postgres (13), then you can use a standard fetch clause:
select *
from mytable
order by id
offset 50 rows fetch next 50 rows only
Note that you do need a unique column (or set of column) in the order by clause to get a stable pagination. I assumed id.
In ealier version, where fetch is not available, one workaround uses window functions:
select *
from (select t.*, row_number() over(order by id) rn from mytable t) t
where rn between 51 and 100

SQL for getting each category data in maria db

I need to fetch 4 random values from each category. What should be the correct sql syntax for maria db. I have attached one image of table structure.
Please click here to check the structure
Should i write some procedure or i can do it with basic sql syntax?
You can do that with a SQL statement if you only have a few rows:
SELECT id, question, ... FROM x1 ORDER BY rand() LIMIT 1
This works fine if you have only a few rows - as soon as you have thousands of rows the overhead for sorting the rows becomes important, you have to sort all rows for getting only one row.
A trickier but better solution would be:
SELECT id, question from x1 JOIN (SELECT CEIL(RAND() * (SELECT(MAX(id)) FROM x1)) AS id) as id using(id);
Running EXPLAIN on both SELECTS will show you the difference...
If you need random value for different categories combine the selects via union and add a where clause
http://mysql.rjweb.org/doc.php/groupwise_max#top_n_in_each_group
But then ORDER BY category, RAND(). (Your category is the blog's province.)
Notice how it uses #variables to do the counting.
If you have MariaDB 10.2, then use one of its Windowing functions.
SELECT column FROM table WHERE category_id = XXX
ORDER BY RAND()
LIMIT 4
do it for all categories

counting rows in select clause with DB2

I would like to query a DB2 table and get all the results of a query in addition to all of the rows returned by the select statement in a separate column.
E.g., if the table contains columns 'id' and 'user_id', assuming 100 rows, the result of the query would appear in this format: (id) | (user_id) | 100.
I do not wish to use a 'group by' clause in the query. (Just in case you are confused about what i am asking) Also, I could not find an example here: http://mysite.verizon.net/Graeme_Birchall/cookbook/DB2V97CK.PDF.
Also, if there is a more efficient way of getting both these results (values + count), I would welcome any ideas. My environment uses zend framework 1.x, which does not have an ODBC adapter for DB2. (See issue http://framework.zend.com/issues/browse/ZF-905.)
If I understand what you are asking for, then the answer should be
select t.*, g.tally
from mytable t,
(select count(*) as tally
from mytable
) as g;
If this is not what you want, then please give an actual example of desired output, supposing there are 3 to 5 records, so that we can see exactly what you want.
You would use window/analytic functions for this:
select t.*, count(*) over() as NumRows
from table t;
This will work for whatever kind of query you have.

implementing paging logic in DB2 SQL

Is there a way to implement paging logic in DB2 SQL, where records can be fetched page wise.
The following query works only for queries with no joins. When queries with join are used the ROW_NUM is returned as 0 and paging cannot be done.
SELECT * FROM (SELECT ROWNUMBER() OVER() AS ROW_NUM, Results.*
FROM (SELECT * FROM Table1 ) AS Results) AS PagedResults
WHERE PagedResults.ROW_NUM>0 AND PagedResults.ROW_NUM<=10
Thanks in advance
How to query range of data in DB2 with highest performance?

How to find the item index number in a query result

is there a simple way to get the number of the current item in a simple SELECT? I need to deliver a column based on a calculation that involves the number of the current index in the select. I am simplifing my problem to an extreme, but roughly speaking, here is an example:
SELECT column1 * ITEMINDEX FROM table
I hope I am being clear. I am using SQL Server. Thank you.
In SQL Server 2005+:
SELECT m.*, ROW_NUMBER() OVER (ORDER BY column) AS rn
FROM mytable m
SQL does not have concept of implicit row number, that's why you need ORDER BY clause to define the order of rows.