Is there any replacement for ROWNUM in Oracle? - oracle

I have JPA Native queries to an Oracle database. The only way I know to limit results is using 'rownum' in Oracle, but for some reason, query parser of a jar driver I have to use does not recognize it.
Caused by: java.sql.SQLException: An exception occurred when executing the following query: "/* dynamic native SQL query */ SELECT * from SFDC_ACCOUNT A where SBSC_TYP = ? and rownum <= ?". Cause: Invalid column name 'rownum'. On line 1, column 90. [parser-2900650]
com.compositesw.cdms.services.parser.ParserException: Invalid column name 'rownum'. On line 1, column 90. [parser-2900650]
How can I get rid of that?

ANSI Standard would be something like the following
SELECT *
FROM (
SELECT
T.*,
ROW_NUMBER() OVER (PARTITION BY T.COLUMN ORDER BY T.COLUMN) ROWNUM_REPLACE
FROM TABLE T
)
WHERE
1=1
AND ROWNUM_REPLACE < 100
or you could also use the following:
SELECT * FROM TABLE T
ORDER BY T.COLUMN
OFFSET 0 ROWS
FETCH NEXT 100 ROWS ONLY;

Related

Invalid number exception when using CASE in view?

There is a table called USER_SETUP. This table has a column called EMPLOYEE_ID which is originally VARCHAR. I created a view to convert the VARCHAR data type to NUMBER. I used TO_DECIMAL("EMPLOYEE_ID",12,0) AS "EMPLOYEE_ID" (because when I use TO_INTEGER("EMPLOYEE_ID") AS "EMPLOYEE_ID", I am getting exception in view).
The view is created using nested SQL statement [e.g: select * from (select * from table)] The view is generating correct output with correct data. In the view I created 2 columns using CASE function which is based on the SQL code inside. Refer the code of SQL View below
Main View code:
CREATE VIEW ECLINIC_KNG.VIEW_USER_SETUP AS
SELECT
USER_ID,
EMPLOYEE_ID,
CASE
WHEN EMPLOYEE_ID < 50000 THEN 'Kuwaiti'
WHEN EMPLOYEE_ID >= 50000 AND LENGTH(EMPLOYEE_ID) <=6 THEN 'Non-Kuwaiti'
ELSE 'NOT KNG'
END AS "KWT_NKWT",
CASE
WHEN LENGTH(EMPLOYEE_ID) <=6 THEN 'KNG'
WHEN LENGTH(EMPLOYEE_ID)=12 THEN 'MOH'
ELSE 'Undefined'
END AS "KNG_MOH",
USER_NAME,
ACTIVE_DATE,
DEACTIVE_DATE,
ACTIVE_STATUS,
USER_CODE,
USER_LABEL,
USER_PASSWORD,
USER_TYPE,
OFFICE_ID,
USER_DESIG,
USER_LICENSE,
USER_SIGN,
CLIMIT_DAYS,
EDIT_BILL_SERVICE,
PASSWORDGROUP_ID,
USERLABEL_AR,
DISCOUNT_APPROVAL,
DISCOUNT_TYPE,
DISCOUNT_MAX,
BILL_CANCELLATION,
BILL_CANCELLATION_DAYS,
DOCTOR_ID
FROM
(SELECT
"USER_ID",
--TO_INTEGER("EMPLOYEE_ID") AS "EMPLOYEE_ID" ,
TO_DECIMAL("EMPLOYEE_ID",12,0) AS "EMPLOYEE_ID",
"USER_NAME",
"ACTIVE_DATE",
"DEACTIVE_DATE",
"ACTIVE_STATUS",
"USER_CODE",
"USER_LABEL",
"USER_PASSWORD",
"USER_TYPE",
"OFFICE_ID",
"USER_DESIG",
"USER_LICENSE",
"USER_SIGN",
"CLIMIT_DAYS",
"EDIT_BILL_SERVICE",
"PASSWORDGROUP_ID",
"USERLABEL_AR",
"DISCOUNT_APPROVAL",
"DISCOUNT_TYPE",
"DISCOUNT_MAX",
"BILL_CANCELLATION",
"BILL_CANCELLATION_DAYS",
"DOCTOR_ID"
FROM
"ECLINIC_KNG"."USER_SETUP"
WHERE
"USER_SETUP"."EMPLOYEE_ID" not in ('undefined', '299450','34(NEW RECRUIT)', 'army', 'MOH
Nurse','NEW RECRUITMENT 380','1111111','0')
AND
"USER_SETUP"."EMPLOYEE_ID" LIKE_REGEXPR '[0-9]'
ORDER BY
"USER_ID");
The 2 new columns created in the view
CASE
WHEN EMPLOYEE_ID < 50000 THEN 'Kuwaiti'
WHEN EMPLOYEE_ID >= 50000 AND LENGTH(EMPLOYEE_ID) <=6 THEN 'Non-Kuwaiti'
ELSE 'NOT KNG'
END AS "KWT_NKWT",
CASE
WHEN LENGTH(EMPLOYEE_ID) <=6 THEN 'KNG'
WHEN LENGTH(EMPLOYEE_ID)=12 THEN 'MOH'
ELSE 'Undefined'
END AS "KNG_MOH"
The issue is when I am filtering specific data using these 2 SQL scripts
--(1)
SELECT * FROM VIEW_USER_SETUP vus WHERE vus.KNG_MOH LIKE 'MOH'; --Issue
--(2)
SELECT * FROM VIEW_USER_SETUP vus WHERE vus.KWT_NKWT LIKE 'NOT KNG'; --Issue
The first issue the output is as follows:
SAP DBTech JDBC: [339]: invalid number: exception 71000339: SQL Error
The second issue the output is a follows:
invalid number: [6930] attribute value is not a number;exception 70006930: attribute value is not a number
To temporary solve 1, I use the below code:
SELECT * FROM VIEW_USER_SETUP vus WHERE vus.KNG_MOH NOT LIKE 'KNG';
Similarly to temporarily solve 2, I use the same above code because of the realtion.
I believe the issue is originating from the two CASE expressions created in the main view code, because when I use other filters from view there is no issue.
What is the correction required in the Main View code such that, when I execute --(1) and --(2), I get the required output?
Require help.

Limit select count subquery work in 21.4.5.46 version but can not work in 21.10.2.15

Limit select count subquery work in 21.4.5.46 version but can not work in 21.10.2.15
Sql is
select * from mytable order by sid limit (select toInt64(count(cid)*0.01) from mytable);
The sql can work very well in in 21.4.5.46 version but can not work in 21.10.2.15.
Exception is : [1002] ClickHouse exception, message: Code: 440. DB::Exception: Illegal type Nullable(Int32) of LIMIT expression, must be numeric type. (INVALID_LIMIT_EXPRESSION) (version 21.10.2.15 (official build))
How to reproduce
1 create table sql:
create table mytable(cid String,create_time String,sid String)engine = MergeTree PARTITION BY sid ORDER BY cid SETTINGS index_granularity = 8192;
2 execute sql
select * from mytable order by sid limit (select toInt64(count(cid)*0.01) from mytable);
ClickHouse release v21.9, 2021-09-09
Backward Incompatible Change
Now, scalar subquery always returns Nullable result if it's type can be Nullable. It is needed because in case of empty subquery it's result should be Null. Previously, it was possible to get error about incompatible types (type deduction does not execute scalar subquery, and it could use not-nullable type). Scalar subquery with empty result which can't be converted to Nullable (like Array or Tuple) now throws error. Fixes #25411. #26423 (Nikolai Kochetov).
Now you should use
SELECT *
FROM mytable
ORDER BY sid ASC
LIMIT assumeNotNull((
SELECT toUInt64(count(cid) * 0.01)
FROM mytable
))
Query id: e3ab56af-96e4-4e01-812d-39af945d7878
Ok.
0 rows in set. Elapsed: 0.004 sec.

Issue in Jqgrid pagination in Oracle server

We have a code to sort data and paginate the same and render the data to a Jqgrid. The code works fine when it is connected to an SQL server. That is on paginating each page returns distinct data as expected. But on connecting to an oracle server after some point of time the duplicate data are rendered. Both Oracle and SQL server has same data. Parameters in the Jqgrid page and the number of pages are working as expected on the server-side. That is on paging the start point and chunk size is correctly transferred to the server-side. The duplicate values are observed after sorting columns that are of type varchar in the database but hold numeric also. The database status column holds values of 3 and A, after sorting with the status column the duplicate data when the paginating issue is observed. Duplicate data in the sense, that data on page 2 will be the same as data on page 3. Any help will be appreciated. Thanks in advance...
Query One:-
select * from ( select row_.*, rownum rownum_ from ( Select x,y,z,status FROM tablename c WHERE status IN('in condition seperated with status') ORDER BY status asc ) row_ where rownum <= 30 ) where rownum_ > 20;
Query Two:-
select * from ( select row_.*, rownum rownum_ from ( Select x,y,z,status FROM tablename c WHERE status IN('in condition seperated with status') ORDER BY status asc ) row_ where rownum <= 20 ) where rownum_ > 10;
Here the query 1 and 2 always return the same results.
Where two or more values in the column of your ORDER BY clause are the same, you must always provide another secondary column to rank. Otherwise, data return has only a probability of fetching correct result as we expect. The possibility of getting a correct answer will be same as rolling a dice. The secondary column must be unique for accurate results. While you might be able to assume that they will sort themselves based on order entered
select * from( select row_.*, rownum rownum_ from( Select x,y,z,status FROM tablename c WHERE status IN('in condition seperated with status') ORDER BY status,x asc ) row_ where rownum <= 30 ) where rownum_ > 20;
Hoping x is a unique value. DBMS_RANDOM.VALUE can also be used in case if is an oracle specific query other than adding extra order by clause

Error in Hive : For Exists/Not Exists operator SubQuery must be Correlated

select * from students1;
students1.name students1.age students1.gpa
fred 35 1.28
barney 32 2.32
shyam 32 2.32
select * from students2;
students1.name students1.age
fred 35
barney 32
When I am running this query
select
name,age from students1
where not exists
(select name,age from students2);
I am getting this bellow error
Error while compiling statement: FAILED: SemanticException line 39:22
Invalid SubQuery expression 'age' in definition of SubQuery sq_1 [
exists (select name,age from students2) ] used as sq_1 at Line 3:10:
For Exists/Not Exists operator SubQuery must be Correlated.
The error message is clear. The subquery should be correlated when using exists/not exists.
select name,age
from students1 s1
where not exists (select 1
from students2 s2
where s1.name=s2.name and s1.age=s2.age
)
You are trying to achieve a MINUS output of a query. It is unfortunately not available in Hive.
You can read through the limitations of HQL and SQL here. HQL vs SQL
For usage of not exists, the manual has good example.
subqueries in hive
MINUS is now available in Hive. You can achieve this as the following:
select name,age from students1
MINUS
select name,age from students2;

How to set range for limit clause in hive

How to set range for limit clause in hive , I have tried the below query but failed with syntax error . Can someone please help
select * from table limit 1000,2000;
You can use Row_Number window function and set the range limit.
Below Query will result only the first 20 records from the table
hive> select * from
(
SELECT *,ROW_NUMBER() over (Order by id) as rowid FROM <tab_name>
)t
where rowid > 0 and rowid <=20;
Using Between operator to specify range
hive> select * from
(
SELECT *,ROW_NUMBER() over (Order by id) as rowid FROM <tab_name>
)t
where rowid between 0 and 20;
To fetch rows from 20 to 40 then increase the lower/upper bound values
hive> select * from
(
SELECT *,ROW_NUMBER() over (Order by id) as rowid FROM <tab_name>
)t
where rowid > 20 and rowid <=40;
The LIMIT clause is used to set a ceiling on the number of rows in the result set. You are getting a syntax error because of an incorrect usage of this HQL clause.
The query could be written as the following to return no more than 2000 rows:
SELECT * FROM table LIMIT 2000;
You could also write it like so to return no more than 1000 rows:
SELECT * FROM table LIMIT 1000;
However you cannot combine both into the same argument for LIMIT. The LIMIT argument must evaluate to a constant value.
I will try and expand on this information a bit to try and help solve your problem. If you are attempting to "paginate" your results the following may be of use.
FIRST I would recommend against leaning on HQL for pagination, in most situations that would be more efficiently implemented on the application logic side (query large result set, cache what you need, paginate with application logic). If you have no choice but to pull out ranges of rows you can get the desired effect through a combination of the LIMIT, ORDER BY, and OFFSET clauses.
LIMIT : This will limit your result set to a maximum number of row
ORDER BY: This will sort/order your result set based on one or more columns
OFFSET: This will start your result set at a certain row after the logical first entry in the table.
You may combine these three clauses to effectively query "pages" of your table. For example the following three queries show how to get the first 3 blocks of data from a table where each block contains 1000 rows and the target table's 'column1' is used to determine logical order.
SELECT title as "Page 1", column1, column2, ... FROM table
ORDER BY column1 LIMIT 1000 OFFSET 0;
SELECT title as "Page 2", column1, column2, ... FROM table
ORDER BY column1 LIMIT 1000 OFFSET 1000;
SELECT title as "Page 3", column1, column2, ... FROM table
ORDER BY column1 LIMIT 1000 OFFSET 2000;
Each query declares 'column1' as the sorting value with ORDER BY. The queries will return no more than 1000 rows due to the LIMIT clause. Each result set will start at a different row due to the OFFSET being incremented by the "page size" for each query.
I am not sure what you are trying to achieve, but ...
That will return the 1001 and the 2001 record in the query results set only if you are using hive a hive version greater than 2.0.0
hive --version
(https://issues.apache.org/jira/browse/HIVE-11531)
Limit in Hive gives 'n' number of records randomly. It's not to print a range of records.
You may use order by in conjunction with limit to get what you want

Resources