implementing paging logic in DB2 SQL - 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?

Related

sql slow postgresql dbeaver

I am using DBeaver to query a PostgreSQL database.
I have this query, it simply selects the highest id per Enterprise_Nbr. The query works but is really slow. Is there any way I can rewrite the query to improve performance.
I am using the querytool DBeaver because I don't have direct access to PostgreSQL. The ultimate goal is to link the PostgreSQL with PowerBi.
select *
from public.address 
where "ID"  in (select max("ID")
from public.address a 
group by "Enterprise_Nbr")
Queries for greatest-n-per-group problems are typically faster if done using Postgres' proprietary distinct on () operator
select distinct on ("Enterprise_Nbr") *
from public.address
order by "Enterprise_Nbr", "ID" desc;
Your query could rewrite as: per each value of Enterprise_Nbr, retrieve row which there is not exists other rows that have same Enterprise_Nbr and greater ID.
SELECT *
FROM public.address a
WHERE NOT EXISTS (
SELECT 1
FROM public.address b
WHERE b.Enterprise_Nbr = a.Enterprise_Nbr AND b.ID > a.ID
)

SQL simple GROUP BY query

Is there a way to make a simple GROUP BY query with SQL and not use COUNT,AVG or SUM? I want to show all columns and group it with a single column.
SELECT * FROM [SPC].[dbo].[BoardSFC] GROUP BY boardsn
The query above is working on Mysql but not on SQL, is there a way to achieve this? any suggestion would be great
UPDATE: Here is my data I just need to group them by boardsn and get imulti equals to 1
I thing you just understand 'group data' in a different way than it is implemented in sql server. You simply want rows that have the same value together in the result and that would be ordering not grouping. So maybe what you need is:
SELECT *
FROM [SPC].[dbo].[BoardSFC]
WHERE imulti = 1
ORDER BY boardsn
The query above is working on Mysql but not on SQL, is there a way to achieve this? any suggestion would be great
No, there is not. MySQL only lets you do this because it violates the various SQL standards quite egregiously.
You need to name each column you want in the result-set whenever you use GROUP BY. The SELECT * feature is only provided as a convenience when working with data interactively - in production code you should never use SELECT *.
You could use a TOP 1 WITH TIES combined with a ORDER BY ROW_NUMBER.
SELECT TOP 1 WITH TIES *
FROM [SPC].[dbo].[BoardSFC]
ORDER BY ROW_NUMBER() OVER (PARTITION BY boardsn ORDER BY imulti)
Or more explicitly, use ROW_NUMBER in a sub-query
SELECT *
FROM
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY boardsn ORDER BY imulti) as RN
FROM [SPC].[dbo].[BoardSFC]
) q
where RN = 1

Select middle rows in 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;

Oracle 11g and SQL TOP query

While using SELECT TOP 5 * FROM SOMETABLE gives me an error
ORA-00923: FROM keyword not found where expected
I am using Oracle 11g . I am aware of using rownum for doing the same thing but just wondering SQL TOP usage is not at all supported in Oracle ? Anything need to do extra to make SQL TOP working in Oracle ??
Oracle does not support TOP. Use ROWNUM
SELECT * FROM your_table
WHERE ROWNUM <= 5
SQLFiddle example
No, Oracle does not support TOP.
As you point out, the best approach is to use rownum. Another option is the analytical function ROW_NUMBER.
The rownum keyword, while it gets you the said no. of records, does so only after applying the order by clause if you have one.
So if the SQL server query is as below, it will give you 10 most recently created records.
Select TOP 10 * from mytable order by created_date desc
But to fit Oracle, when you write this, it gets you the 10 records (that may not be the most recent ones) and arranges them in descending order, which is not what you wanted.
Select * from mytable where rownum < 10 order by created_date desc
So writing with an additional select like this would help:
SELECT * FROM (Select * from mytable order by created_date desc) where rownum < 10
SQL TOP does NOT work for Oracle.

sql Fetch records from sql server database table in parts

I want to fetch records from a table in my sql server database in parts. Like, in one query I want to see first 1000 records, in next query next 1000 records. Likewise..
Is it possible with sql server ? I am using sql server 2008. While googling, I found LIMIT clause for mysql, but it does not work for sql server. So can any one give in Sql. Please help.
First 1000 records:
SELECT TOP 1000 *
FROM mytable
ORDER BY
mycolumn
General solution (supports offset)
SELECT *
FROM (
SELECT *,
ROW_NUMBER() OVER (ORDER BY mycolumn) rn
FROM mytable
) q
WHERE rn BETWEEN 1001 AND 2000
ORDER BY
mycolumn
Have also a look at T-sql: how to perform optimised Paging?