Error in Join query with pagination for Oracle 10g - sql

I'm executing this query in SqlDeveloper targeting a database in Oracle 10g:
select P1.FIELD1, P1.COD from
(select P1.FIELD1, P1.COD, row_number()
over (order by P1.FIELD1) r from
SCHEMA1.P2
INNER JOIN SCHEMA1.P1 on SCHEMA1.P1.COD=P2.FIELD2)
where P1.FIELD_11 = 'VALUE1' and r between 6 and 10;
And Oracle returns:
Error SQL: ORA-00904: "P1"."FIELD_11": invalid identifier
I tried:
select SCHEMA1.P1.FIELD1, SCHEMA1.P1.COD from
(select SCHEMA1.P1.FIELD1, SCHEMA1.P1.COD, row_number()
over (order by SCHEMA1.P1.FIELD1) r from
SCHEMA1.P2
INNER JOIN SCHEMA1.P1 on SCHEMA1.P1.COD=SCHEMA1.P2.FIELD2)
where SCHEMA1.P1.FIELD_11 = 'VALUE1' and r between 6 and 10;
And I got the same error:
ORA-00904: "SCHEMA1"."P1"."FIELD_11": invalid identifier
I cannot find examples of select clauses with inner join and pagination for Oracle 10g, so I don't know what I'm doing wrong.

You have messed the query with aliases which makes it confusing.use shorter aliases instead of whole schema.table.column everywhere
SELECT p.field1,
p.cod
FROM (SELECT p1.field1,
p1.cod,
p1.field_11,
row_number()
over (
ORDER BY p1.field1) r
FROM schema1.p2 p2
inner join schema1.p1 p1
ON p1.cod = p2.field2) p
WHERE p.field_11 = 'VALUE1'
AND r BETWEEN 6 AND 10;

Related

ParseException line cannot recognize input near '(' 'select' 'from' in joinSource

I am trying to execute a query in hive and getting the error. I check over and over again but I cannot see any problem.
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) ) a
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no
You missed aliasing first innermost subquery. Correct SQL below -
select
a.phone_no,
a.app_name
from
(select * from (select app_name,phone_no from lc_app_flag) subq) a --inner subquery as subq
inner join
(select * from (select phone_no,city_id_day,city_id_night,lat_day,lng_day,lat_night,lat_night from TW_FEATS_FIN_LCEXT01 where month_id='202205' ) b where city_id_day='440100' or city_id_night='440100') c
on a.phone_no=c.phone_no

Oracle SQL COUNT in an EXISTS SELECT

I want to create a query where I get all posts from my table INV where the INNUM exists more than 2 times in table INVS.
This is my query right now, but it fails with the typical "missing right parenthesis" error.
But when I run the EXISTS Query isolated, it works....
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN WO ON INV.WOID = WO.WOID
WHERE EXISTS (
SELECT COUNT(*) FROM INVS WHERE INVS.INNUM = INV.INNUM and INVS.SITEID='ARZ' GROUP BY INVS.INNUM
HAVING COUNT(*) > 2 ORDER BY INVS.INNUM
);
I dont really know why!?
Hmmm . . . use a scalar subquery to calculate the count and compare to "2" in the outer query:
SELECT WO.WONUM, WO.DESCRIPTION, INV.INNUM, INV.STATUSDATE
FROM INV LEFT OUTER JOIN
WO
ON INV.WOID = WO.WOID
WHERE (SELECT COUNT(*)
FROM INVS
WHERE INVS.INNUM = INV.INNUM AND
INVS.SITEID = 'ARZ'
) > 2;
Your query is relying on a doubly nested correlation clause which Oracle does not support.
You could also move the subquery to the FROM clause, but this version is more in the spirit of how you have written the query.
You get ORA-00907: missing right parenthesis while using the ORDER BYclause in the subquery.
Remove it and you get a valid syntax
Example
with tab as (select rownum id from dual connect by level <= 5
union all
select 3 from dual union all
select 5 from dual)
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1)
;
ID
----------
3
5
3
5
This is not a valid syntax --> ORA-00907: missing right parenthesis
select * from tab t
where exists
(select count(*) from tab
where id = t.id
group by id
having count(*) > 1 order by id)

how to set expression variable in query select oracle sql

I have Oracle SQL like these :
SELECT
z."date", z.id_outlet as idOutlet, z.name as outletName, z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
z.actual,rownumber = tartot + rownumber as targetTotal
FROM (SELECT
b.visit_date as "date", a.id_outlet, max(o.name) as name, max(a.target_sales) as targetBulanan, a.id_material_class as matClass,
max(x.totalVisit) as totalVisit, NVL(SUM(d.billing_value),0) as actual
FROM (
select * from target_bulanan
where deleted = 0 and enabled = 1 and id_salesman = :id_salesman AND id_material_class like :id_material_class AND id_outlet like :id_outlet AND month = TO_NUMBER(TO_CHAR(current_date,'mm')) and year = to_number(TO_CHAR(current_date,'YYYY'))
) a
INNER JOIN outlet o ON o.id_outlet = a.id_outlet
LEFT JOIN visit_plan b ON b.deleted = 0 and a.id_salesman = b.id_salesman AND a.month = TO_NUMBER(TO_CHAR(b.visit_date,'mm')) AND a.year = to_number(TO_CHAR(b.visit_date,'yyyy')) AND a.id_outlet = b.id_outlet
LEFT JOIN so_header c ON SUBSTR(c.id_to,'0',1) = 'TO' AND a.id_salesman = c.id_salesman AND a.id_outlet = c.id_outlet
LEFT JOIN assign_billing d ON c.no_so_sap = d.no_so_sap AND d.billing_date = b.visit_date AND a.id_material_class = (SELECT id_material_class FROM material WHERE id = d.id_material)
LEFt JOIN (SELECT id_salesman, to_char(visit_date,'mm') as month, to_char(visit_date,'yyyy') as year, id_outlet, COUNT(*) as totalVisit FROM visit_plan
WHERE deleted = 0
group by id_salesman, id_outlet,to_char(visit_date,'mm'), to_char(visit_date,'yyyy')) x on
x.id_salesman = a.id_salesman AND x.month = a.month AND x.year = a.year AND x.id_outlet = a.id_outlet
GROUP BY b.visit_date, a.id_outlet, a.id_material_class) z
CROSS JOIN (SELECT 0 as rownumber FROM DUAL ) r
CROSS JOIN (SELECT 0 as tartot FROM DUAL ) t
CROSS JOIN (SELECT '' as mat FROM DUAL ) m
CROSS JOIN (SELECT '' as outlet FROM DUAL ) o
ORDER by outletName, z.matClass, z."date"
I want value of rownumber is formula in my select query but the result is error with this message
ORA-00923: FROM keyword not found where expected
00923. 00000 - "FROM keyword not found where expected"
Anyone can help me ? thanks
Just for enumeration -
replace the line
rownumber = rownumber + 1 AS row_number
with this
rownum AS row_number
rownum is an Oracle inbuilt function that enumerates each record of the result set and with auto increments
As mentioned by Gordon Linoff in his answer, there are further problems in your query.
At the first look (without executing it), I could list the problematic lines -
AND month = TO_NUMBER(TO_CHAR(current_date,'mm'))
AND year = to_number(TO_CHAR(current_date,'YYYY'))
Instead of current_date use sysdate
LEFT JOIN so_header c ON SUBSTR(c.id_to,'0',1) = 'TO'
I guess, you meant to do this -
LEFT JOIN so_header c ON SUBSTR(c.id_to,0,2) = 'TO'
i.e. substring from index 0 upto 2 characters
Plus, no need of those cross joins
THIS ADDRESSES THE ORIGINAL QUESTION.
You may have multiple problems in your query. After all, the best way to debug and to write queries is to start simple and gradually add complexity.
But, you do have an obvious error. In your outermost select:
SELECT z."date", z.id_outlet as idOutlet, z.name as outletName,
z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
z.actual,
rownumber = rownumber + 1 as row_number
The = is not Oracle syntax -- it looks like a SQL Server extension for naming a column or a MySQL use of variables.
I suspect that you want to enumerate the rows. If so, one syntax is row_number():
SELECT z."date", z.id_outlet as idOutlet, z.name as outletName,
z.matClass, z.targetBulanan, z.targetBulanan/totalVisit as targetAwal,
z.actual,
row_number() over (order by outletName, z.matClass, z."date") as row_number
In Oracle, you could also do:
rownum as row_number

Spark sql subquery

I can't find issue with below query. It keeps complaining about
cannot recognize input near 'SELECT' 'wrk_prd_dt' '.' in expression specification (state=42000,code=40000)
select tb1.name from dept tb1 LEFT JOIN emp lexp ON (lexp.id = tb1.id)
where tb1.prd_dt = (SELECT wrk_prd_dt.prd_dt FROM class wrk_prd_dt order by wrk_prd_dt.prd_dt asc limit 1);
tried Second approach but same error.
SELECT tb1.name
FROM dept tb1
WHERE tb1.prd_dt >= (select min(wrk.prd_dt) from class wrk)
limit 1

conversion failed when converting from a character string to uniqueidentifier sql server 2014

This is my SQL query. While executing this query, i am getting above error
WITH Results_CTE AS (select Top 20 ROW_NUMBER() OVER (ORDER BY KeyId desc) AS SNo,* from(select * from ( select ps.Id as KeyId,dps.Id as SettingId,dps.DocumentType,dps.DocumentProcessingType,null as Description,null as Link
from [ParttimerSalary] ps inner join [ParttimerEmployee] pe on pe.Id = ps.ParttimerEmployeeId
inner Join [DocumentProcessingSetting] dps on dps.Step = ps.Step and dps.KeyId = pe.ParttimerType and
(dps.RoleId = 'e218e9e0-d51d-45b4-9380-77f43ac50f0d' or dps.UserId = '1967681a-7d64-486e-99a8-4b58746cef81') and dps.DocumentType = 2 union all
Select 'Incident/service is assigned for you' as [Description],'#/it/incidentsupport/' as Link,0 as Id,0 as SettingId,0 as DocumentType,0 as DocumentProcessingType from Incident where IncidentStatus = 2 and SupportById = '1967681a-7d64-486e-99a8-4b58746cef81'
)wrapped) listing ORDER BY KeyId desc) SELECT * FROM Results_CTE WHERE SNo >= 0 AND SNo <=20
Any help would be appreciated. Thanks
While using Union all the datatype should match in both the select elements.
ps.Id as KeyId is a unique identifer whereas 'Incident/service is assigned for you' as [Description] is character string that's the reason for error.
Actually, I had changed the order of selecting columns from the table after union keyword, as during union, we need manage the order,datatype, etc of field of the both tables
And the problem was solved.
Anyway thanks