Convert rownum() from db2 to Oracle - sql

Good day,
I would like to change some program code (mostly on SQL code) because the database already change from DB2 to Oracle.
Here is 1 example that I successful changed but I am not really understand about it, and I cant find it from google.
The following is the original SQL Query code (using DB2) :
SELECT *
FROM (SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum
FROM IBSADMIN.CCGROUP T0
WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC') AS tname
WHERE rownum BETWEEN 1 AND 20
Here is the SQL Query code after edit by me (successfully get data from Oracle) :
SELECT *
FROM (SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum1
FROM IBSADMIN.CCGROUP T0
WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC') tname
WHERE rownum1 BETWEEN 1 AND 20
As I analyze, I get error if I didnt change the rownum to rownum1, error is ORA-00923: FROM keyword not found where expected .Thus I change it to rownum1, I think the rownum should be a keyword in DB2, is there any keyword like this for Oracle also?
In the line 4 last part, from DB2 code, it end with As tname. If I put the same things in Oracle code, I get error ORA-00933: SQL command not properly ended . Thus I erase the As . I not so understand how is the As tname means, As the SQL query in the bracket:
(SELECT T0.CREATEDBY AS C1, row_number() OVER ( ORDER BY T0.GROUPNAME) AS rownum1
FROM IBSADMIN.CCGROUP T0
WHERE T0.GROUPID != 0001 AND T0.GROUPID != 001 AND T0.CHANNEL = 'CC')
It return me 2 columns, thus I am not understand how is the As tname interate with the 2 column.
Kindly advise.

rownum is a reserved word in Oracle-- in Oracle, rownum is a pseudocolumn that you can reference to get the row number (before ordering) of a result set. That's why you need to change the alias when you convert to Oracle. I would prefer something that was more obviously different from rownum-- rn or rnk are good options.
tname is an alias for the inline view in your query. In Oracle, you cannot use the AS keyword to assign a table alias (you can optionally use it when defining a column alias which is why AS rownum1 is valid but you could also get rid of the AS entirely). In this case, the tname alias is never used so in Oracle you could omit it. I know in some databases (SQL Server) aliases for inline views are required-- I'm not sure whether DB2 requires the table alias.
It sounds like you modified the query correctly (though I quibble with the alias you chose instead of rownum).

May be nobody is still interested in this, but I found this solution here:
http://www.sqlines.com/db2-to-oracle/fetch_first_rows_only
use FETCH FIRST x ROWS ONLY, something like:
select * from a_table FETCH FIRST 1 ROWS ONLY;
will give you only the first line of the table, I've used this and it worked just as I expected.

Related

SQL Server - Pagination Without Order By Clause

My situation is that a SQL statement which is not predictable, is given to the program and I need to do pagination on top of it. The final SQL statement would be similar to the following one:
SELECT * FROM (*Given SQL Statement*) b
OFFSET 0 ROWS FETCH NEXT 50 ROWS ONLY;
The problem here is that the *Given SQL Statement* is unpredictable. It may or may not contain order by clause. I am not able to change the query result of this SQL Statement and I need to do pagination on it.
I searched for solution on the Internet, but all of them suggested to use an arbitrary column, like primary key, in order by clause. But it will change the original order.
The short answer is that it can't be done, or at least can't be done properly.
The problem is that SQL Server (or any RDBMS) does not and can not guarantee the order of the records returned from a query without an order by clause.
This means that you can't use paging on such queries.
Further more, if you use an order by clause on a column that appears multiple times in your resultset, the order of the result set is still not guaranteed inside groups of values in said column - quick example:
;WITH cte (a, b)
AS
(
SELECT 1, 'a'
UNION ALL
SELECT 1, 'b'
UNION ALL
SELECT 2, 'a'
UNION ALL
SELECT 2, 'b'
)
SELECT *
FROM cte
ORDER BY a
Both result sets are valid, and you can't know in advance what will you get:
a b
-----
1 b
1 a
2 b
2 a
a b
-----
1 a
1 b
2 a
2 b
(and of course, you might get other sorts)
The problem here is that the *Given SQL Statement" is unpredictable. It may or may not contain order by clause.
your inner query(unpredictable sql statement) should not contain order by,even if it contains,order is not guaranteed.
To get guaranteed order,you have to order by some column.for the results to be deterministic,the ordered column/columns should be unique
Please note: what I'm about to suggest is probably horribly inefficient and should really only be used to help you go back to the project leader and tell them that pagination of an unordered query should not be done. Having said that...
From your comments you say you are able to change the SQL statement before it is executed.
You could write the results of the original query to a temporary table, adding row count field to be used for subsequent pagination ordering.
Therefore any original ordering is preserved and you can now paginate.
But of course the reason for needing pagination in the first place is to avoid sending large amounts of data to the client application. Although this does prevent that, you will still be copying data to a temp table which, depending on the row size and count, could be very slow.
You also have the problem that the page size is coming from the client as part of the SQL statement. Parsing the statement to pick that out could be tricky.
As other notified using anyway without using a sorted query will not be safe, But as you know about it and search about it, I can suggest using a query like this (But not recommended as a good way)
;with cte as (
select *,
row_number() over (order by (select 0)) rn
from (
-- Your query
) t
)
select *
from cte
where rn between (#pageNumber-1)*#pageSize+1 and #pageNumber*#pageSize
[SQL Fiddle Demo]
I finally found a simple way to do it without any order by on a specific column:
declare #start AS INTEGER = 1, #count AS INTEGER = 5;
select * from (SELECT *,ROW_NUMBER() OVER (ORDER BY (SELECT 1)) AS fakeCounter
FROM (select * from mytable) AS t) AS t2 order by fakeCounter OFFSET #start ROWS
FETCH NEXT #count ROWS ONLY
where select * from mytable can be any query

Return Row of Every Nth Record

I'm trying to developer the Oracle SQL version of the accepted answer here:
Return row of every n'th record
What I have so far is:
SELECT ROW_ID, CUST_ACCT_SITE_ID
FROM
(
SELECT CUST_ACCT_SITE_ID as CUST_ACCT_SITE_ID, ROW_NUMBER() OVER (ORDER BY CUST_ACCT_SITE_ID) AS ROW_ID
FROM XXDMX_VOICE_CUSTOMERS_TBL
) AS t
WHERE ROW_ID % 10000 = 0
ORDER BY CUST_ACCT_SITE_ID;
I get the error
ERROR
ORA-00933: SQL command not properly ended
I've tried lots of variations and can't think of what I am doing wrong. Any ideas, Oracle experts?
Try writing the query like this:
SELECT rn, CUST_ACCT_SITE_ID
FROM (SELECT CUST_ACCT_SITE_ID as CUST_ACCT_SITE_ID,
ROW_NUMBER() OVER (ORDER BY CUST_ACCT_SITE_ID) AS rn
FROM XXDMX_VOICE_CUSTOMERS_TBL
) t
WHERE mod(rn, 10000) = 0
ORDER BY CUST_ACCT_SITE_ID;
The primary difference is removing the as for the table alias. Oracle doesn't allow this syntax. I also changed row_id to something else, because "rowid" means something in Oracle and its use could be confusing (see here).
In PL/SQL (the name for "Oracle SQL"), the modulus operator uses this syntax:
WHERE MOD(ROW_ID, 10000) = 0

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 query trick

I need to do
select * from xxx where name in (a,b,c...);
but I want the result set to be in the order of (a,b,c...). is this possible?
I found this question which is looks like your original question: Ordering by the order of values in a SQL IN() clause
ah - I see. you could do something horrendous with a case statement, and then order by that.. you'd effectivley be adding another column to your query to be an "order" that you could then "order by"
its ugly, but if you control the query, and the number in the 'in' clause is low, it could work (beleive an 'in' clause is limited to 255 chars)
e.g "IF name = a then 1 else if name = b then 2"
Failing that, probably best to sort in the client using a similar technique (assuming it was the client that injected the information into the 'in' clause in the first place)
-Ace
The method to do this will be DB-specific.
In Oracle, you could do something like:
SELECT * FROM xxx
where name in (a,b,c...)
ORDER BY DECODE(name,a,1,b,2,c,3);
IN statements are pretty limited, but you could get a similar effect by joining on a subquery.
here's an example:
SELECT x.*
FROM xxx as x
INNER JOIN ((select a as name, 1 as ord)
UNION
(select b as name, 2 as ord)
UNION
(select c as name, 3 as ord)) as t
ON t.name = x.name
ORDER BY t.ord
its pretty ugly, but it should work on just about any sql database. The ord field explicitly allows you to set the ordering of the result. some databases such as SqlServer support a ROWINDEX feature so you may be able to use that to clean it up a bit.

Paging with Oracle and sql server and generic paging method

I want to implement paging in a gridview or in an html table which I will fill using ajax. How should I write queries to support paging? For example if pagesize is 20 and when the user clicks page 3, rows between 41 and 60 must be shown on table. At first I can get all records and put them into cache but I think this is the wrong way. Because data can be very huge and data can be change from other sessions. so how can I implement this? Is there any generic way ( for all databases ) ?
As others have suggested, you can use rownum in Oracle. It's a little tricky though and you have to nest your query twice.
For example, to paginate the query
select first_name from some_table order by first_name
you need to nest it like this
select first_name from
(select rownum as rn, first_name from
(select first_name from some_table order by first_name)
) where rn > 100 and rn <= 200
The reason for this is that rownum is determined after the where clause and before the order by clause. To see what I mean, you can query
select rownum,first_name from some_table order by first_name
and you might get
4 Diane
2 Norm
3 Sam
1 Woody
That's because oracle evaluates the where clause (none in this case), then assigns rownums, then sorts the results by first_name. You have to nest the query so it uses the rownum assigned after the rows have been sorted.
The second nesting has to do with how rownum is treated in a where condition. Basically, if you query "where rownum > 100" then you get no results. It's a chicken and egg thing where it can't return any rows until it finds rownum > 100, but since it's not returning any rows it never increments rownum, so it never counts to 100. Ugh. The second level of nesting solves this. Note it must alias the rownum column at this point.
Lastly, your order by clause must make the query deterministic. For example, if you have John Doe and John Smith, and you order by first name only, then the two can switch places from one execution of the query to the next.
There are articles here http://www.oracle.com/technology/oramag/oracle/06-sep/o56asktom.html
and here http://www.oracle.com/technology/oramag/oracle/07-jan/o17asktom.html. Now that I see how long my post is, I probably should have just posted those links...
Unfortunately, the methods for restricting the range of rows returned by a query vary from one DBMS to another: Oracle uses ROWNUM (see ocdecio's answer), but ROWNUM won't work in SQL Server.
Perhaps you can encapsulate these differences with a function that takes a given SQL statement and first and last row numbers and generates the appropriate paginatd SQL for the target DBMS - i.e. something like:
sql = paginated ('select empno, ename from emp where job = ?', 101, 150)
which would return
'select * from (select v.*, ROWNUM rn from ('
+ theSql
+ ') v where rownum < 150) where rn >= 101'
for Oracle and something else for SQL Server.
However, note that the Oracle solution is adding a new column RN to the results that you'll need to deal with.
I believe that both have a ROWNUM analytic Function. Use that and you'll be identical.
In Oracle it is here
ROW_NUMBER
Yep, just verified that ROW_NUMBER is the same function in both.
"Because...data can be change from other sessions."
What do you want to happen for this ?
For example, user gets the 'latest' ten rows at 10:30.
At 10:31, 3 new rows are added (so those ten being view by the user are no longer the latest).
At 10:32, the user requests then 'next' ten entries.
Do you want that new set to include those three that have been bumped from 8/9/10 down to 11/12/13 ?
If not, in Oracle you can select the data as it was at 10:30
SELECT * FROM table_1 as of timestamp (timestamp '2009-01-29 10:30:00');
You still need the row_number logic, eg
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
select *
from ( select /*+ FIRST_ROWS(n) */ a.*,
ROWNUM rnum
from ( your_query_goes_here,
with order by ) a
where ROWNUM <=
:MAX_ROW_TO_FETCH )
where rnum >= :MIN_ROW_TO_FETCH;
Step 1: your query with order by
Step 2: select a.*, ROWNUM rnum from ()a where ROWNUM <=:MAX_ROW_TO_FETCH
Step 3: select * from ( ) where rnum >= :MIN_ROW_TO_FETCH;
put 1 in 2 and 2 in 3
If the expected data set is huge, I'd recommend to create a temp table, a view or a snapshot (materialized view) to store the query results + a row number retrieved either using ROWNUM or ROW_NUMBER analytic function. After that you can simply query this temp storage using row number ranges.
Basically, you need to separate the actual data fetch from the paging.
There is no uniform way to ensure paging across various RDBMS products. Oracle gives you rownum which you can use in where clause like:
where rownum < 1000
SQL Server gives you row_id( ) function which can be used similar to Oracle's rownum. However, row_id( ) isn't available before SQL Server 2005.