With clause subquery availablility for the entire SQL statement in Oracle - sql

I am trying to write a SQL statement which reuses the subquery of the With clause multiple times in Oracle.
With mySubQ as (
...
)
Select Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where mySubQ.Something = SomeTable.Something
This gives me error - ORA-32034 unsupported use of WITH clause
What am I missing?

You need to join with mySubQ, not just define it.
WITH mySubQ AS (...)
SELECT Something
FROM SomeTable
JOIN mySubQ ON mySubQ.Something = SomeTable.Something
WHERE mySubQ.Something >= 0
If you put the query of mySubQ in a subquery, you can't reference mySubQ in the WHERE clause of the main query. Each level of query can only access tables in its own FROM and JOIN clauses, not those of subqueries.

Here is the error: where mySubQ.Something = SomeTable.Something.
The bottom query selects from SomeTable and from the subquery with alias newSubQ,
so mySubQ.Something is not known in this context.
If something is a real column name, not only a "placeholder in the pseudocode", then there is also another error here: Select Something - the column is ambiguous, because both sometable and the subquery have this column.
Try this query:
With mySubQ as (
SELECT * FROM sometable
)
Select newSubQ.Something
From SomeTable,
(
Select *
From mySubQ
where mySubQ.Something >= 0
) newSubQ
where newSubQ.Something = SomeTable.Something
;
Demo --> http://www.sqlfiddle.com/#!4/88855/12
This demo contains also another example of using WITH clause:
WITH mySubQ AS (
SELECT *
FROM sometable
),
mySubQ_1 AS (
SELECT *
FROM mySubQ
WHERE somethingelse = 1
),
mySubQ_2 AS (
SELECT *
FROM mySubQ
WHERE something between 2 AND 5
)
SELECT *
FROM sometable s, mySubQ_1 m1,
(
SELECT * FROM mySubQ_2
WHERE something < 10
) m2
WHERE s.something = m1.something
AND m1.somethingelse = m2.somethingelse

Related

"ORA-00923: FROM keyword not found where expected\n what should I fix

I have an oracle query as follows but when I make changes to pagination the results are different. what should i pass for my code
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select a.KODE_KLAIM,
a.NO_SS,
a.LA,
a.NAMA_TK,
a.KODE_K,
(
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
) TEM_LAHIR,
(
select TO_CHAR(tk.TLAHIR, 'DD/MM/RRRR')
from KN.VW_KTK tk
where tk.KODE_K = a.KODE_K
and rownum=1
) TLAHIR
from PN.KLAIM a
where nvl(a.STATUS_BATAL,'X') = 'T'
and A.NOMOR IS NOT NULL
and A.TIPE_KLAIM = 'JPN01'
)b
)
where 1 = 1
WHERE ROWNUM < ( ( ? * ? ) + 1 )
WHERE r__ >= ( ( ( ? - 1 ) * ? ) + 1 )
but i run this query i have result ORA-00900: invalid SQL statement
You have three WHERE clauses at the end (and no ORDER BY clause). To make it syntactically valid you could change the second and third WHERE clauses to AND.
However, you mention pagination so what you probably want is to use:
SELECT *
FROM (
SELECT b.*,
ROWNUM r__
FROM (
select ...
from ...
ORDER BY something
)b
WHERE ROWNUM < :page_size * :page_number + 1
)
WHERE r__ >= ( :page_number - 1 ) * :page_size + 1
Note: You can replace the named bind variables with anonymous bind variables if you want.
Or, if you are using Oracle 12 or later then you can use the OFFSET x ROWS FETCH FIRST y ROWS ONLY syntax:
select ...
from ...
ORDER BY something
OFFSET (:page_number - 1) * :page_size ROWS
FETCH FIRST :page_size ROWS ONLY;
Additionally, you have several correlated sub-queries such as:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K and rownum=1
This will find the first matching row that the SQL engine happens to read from the datafile and is effectively finding a random row. If you want a specific row then you need an ORDER BY clause and you need to filter using ROWNUM AFTER the ORDER BY clause has been applied.
From Oracle 12, the correlated sub-query would be:
select tk.TEM_LAHIR
from KN.VW_KN_TK tk
where tk.KODE_K = a.KODE_K
ORDER BY something
FETCH FIRST ROW ONLY

simple subquery not working DB2

hey guys this is a very simple sql query that is not giving me the correct result.
subquery:
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
this subquery successfully returns a correct value 627809
simple query:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (627809)
this query executes properly and returns 4 rows.(4 addresses for a member)
but if I try to combine these queries in 1 query as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
then the query returns 0 rows. why is this happening?
Thanks
Your query looks OK, the only I can think is maybe you mistake the value on the result.
can you try this:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)
and this
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID IN (
SELECT 627809
FROM NEODB2ADMIN.ORDERS
)
Since your Order (presumably) can only carry a single Member_ID -- can you please try your full query without the "IN", rather try an equal join as follows:
SELECT *
FROM NEODB2ADMIN.ADDRESS
WHERE MEMBER_ID = (
SELECT NEODB2ADMIN.ORDERS.MEMBER_ID
FROM NEODB2ADMIN.ORDERS
WHERE NEODB2ADMIN.ORDERS.ORDERS_ID = 6371043
)

Hierarchical query outputs error

Here is my code:
with tbl as
(
Select parent_b.BRANCH_ID, parent_b.BRANCH_CODE,
parent_b.BRANCH, parent_b.PARENT_BRANCH_ID
from table parent_b
Where parent_b.branch_Id = 1
UNION ALL
Select child_b.BRANCH_ID, child_b.BRANCH_CODE,
child_b.BRANCH, child_b.PARENT_BRANCH_ID
from table child_b
INNER JOIN tbl parent
ON parent.branch_id = child_b.branch_id
)
select * from tbl
OPTION(MAXRECURSION 32767)
Above code gets me error with this message:
The statement terminated. The maximum recursion 100 has been exhausted
before statement completion.
I checked the table, and there no rows where row points to himself (infinity).
Where is my problem?
There's a logical error in the recursive part of your CTE - the join condition:
ON parent.branch_id = child_b.branch_id
is linking the parent's branch_id directly to the child's branch_id, when it should be linking to the child's parent_branch_id - like so:
ON parent.branch_id = child_b.parent_branch_id
The following query should work:
with tbl as
(
Select parent_b.BRANCH_ID, parent_b.BRANCH_CODE,
parent_b.BRANCH, parent_b.PARENT_BRANCH_ID
from table parent_b
Where parent_b.branch_Id = 1
UNION ALL
Select child_b.BRANCH_ID, child_b.BRANCH_CODE,
child_b.BRANCH, child_b.PARENT_BRANCH_ID
from table child_b
INNER JOIN tbl parent
ON parent.branch_id = child_b.parent_branch_id
)
select * from tbl
OPTION(MAXRECURSION 32767)
Specify the maxrecursion option at the end of the query:
select *
from tbl
option (maxrecursion 0)

Why colums in SELECT not belongs to SELECT

I have this select, but does not work.
select
a.code1,
a.data1,
a.stval,
(select sum(col1+col2+col3) from tad ) as sum1,
(select sum(col7+col8+col9) from tbac) as sum2,
CASE
WHEN (sum1+sum2) > 100 THEN (a.stval * sum1)
WHEN (sum1+sum2( <= 100 THEN (a.stval * sum2)
END as newdat1
from arti as a
Where is the error? why (sum1+sum2) its error?
Thanks
(sum1 + sum2) is an error because these identifiers are not defined in the scope where you are trying to use them. In an SQL select list, you cannot use symbols declared in the same select list, irrespective of their position on the list. Use a subquery if you need to access sum1 and sum2.
The specific reason is that SQL is a descriptive language that does not guarantee the order of evaluation of expressions. This is true in the select clause. This is true in the where clause. It is true in the from clause. SQL describes what the results look like. It does not prescribe the specific actions.
As a result, SQL does not allow identifiers defined in the select to be used in the same select clause (nor in the where clause at the same level). The expressions can be processed in any order.
The normal solution in your case is to use a subquery or a CTE. In your case, though, the subqueries are independent of the outer query (as written), so I would move them to the from clause:
select a.code1, a.data1, a.stval, x1.sum1, x2.sum2,
(CASE WHEN x1.sum1 + x2.sum2 > 100 THEN a.stval * x1.sum1
WHEN x1.sum1 + x2.sum2 <= 100 THEN a.stval * x2.sum2
END) as newdat1
from arti a cross join
(select sum(col1+col2+col3) as sum1 from tad ) x1 cross join
(select sum(col7+col8+col9) as sum2 from tbac) x2;
EDIT:
You can use a subquery or CTE. But there is an approach that builds on the above:
select a.code1, a.data1, a.stval, x1.sum1, x2.sum2,
(CASE WHEN x1.sum1 + x2.sum2 > 100 THEN a.stval * x1.sum1
WHEN x1.sum1 + x2.sum2 <= 100 THEN a.stval * x2.sum2
END) as newdat1
from arti a join
(select ascon, sum(col1+col2+col3) as sum1
from tad
group by ascon
) x1
on x1.ascon = arti.code1 cross join
(select sum(col7+col8+col9) as sum2 from tbac) x2;

Incorrect Syntax Near Keyword 'OPTION' in CTE Statement

I get a "Incorrect syntax near keyword 'OPTION'" error when attempting to save a view in SQL Server 2008. I'm trying to add a MAXRECURSION option to the end of my common table expression. All the examples I've seen and working CTEs I've created before don't mind the "OPTION (MAXRECURSION 0)" at the end of the CTE.
Anyone see why I'm getting this error? The CTE works without the OPTION clause, although it reaches the maximum number of recursions (100).
WITH CTE AS
(
SELECT
CDay,
InvAcct,
BuyerCode,
PartNumber,
ROP,
ROP_ROQ,
DailyDemand,
StartingInvQty,
SchedDeliveryQty,
CAST(StartingInvQty - DailyDemand/2.0 AS decimal(18,4)) AS ProjInvQty
FROM
qryInventorySimulation
WHERE
MBC = 'B' AND
CDay = CAST(CAST(GETDATE()AS date) as datetime)
UNION ALL
SELECT
qryInventorySimulation.CDay,
qryInventorySimulation.InvAcct,
qryInventorySimulation.BuyerCode,
qryInventorySimulation.PartNumber,
qryInventorySimulation.ROP,
qryInventorySimulation.ROP_ROQ,
qryInventorySimulation.DailyDemand,
qryInventorySimulation.StartingInvQty,
qryInventorySimulation.SchedDeliveryQty,
CAST(CTE.ProjInvQty + qryInventorySimulation.SchedDeliveryQty - qryInventorySimulation.DailyDemand AS decimal(18,4)) AS ProjInvQty
FROM
qryInventorySimulation INNER JOIN CTE ON qryInventorySimulation.InvAcct = CTE.InvAcct AND qryInventorySimulation.PartNumber = CTE.PartNumber AND qryInventorySimulation.CDay = DATEADD(d,1,CTE.CDay)
WHERE
qryInventorySimulation.CDay <= DATEADD(d,120,GETDATE())
)
SELECT * FROM CTE
OPTION (MAXRECURSION 0);
You can't apply this option within the view. You would need to apply it to the query calling the view. e.g.
CREATE VIEW dbo.V
AS
WITH CTE AS
( SELECT 1 AS A
UNION ALL
SELECT A + 1
FROM CTE
WHERE A < 50
)
SELECT *
FROM CTE;
GO
SELECT *
FROM dbo.V
OPTION (MAXRECURSION 0);
If you think of a view more as a stored subquery than a stored query (yes it can be called on it's own but it is not necessarily), and remember that its definition is expanded out into the main query (unless you are using NOEXPAND - which you could not on a view that contained a recursive CTE anyway), so in essence you are trying to do something like this:
WITH RecursiveCTE AS (...)
SELECT *
FROM T
INNER JOIN
( SELECT *
FROM RecursiveCTE
OPTION (MAXRECURSION 0)
) c
ON c.SomeField = T.SomeField;
Whereas the correct syntax would be:
WITH RecursiveCTE AS (...)
SELECT *
FROM T
INNER JOIN
( SELECT *
FROM RecursiveCTE
) c
ON c.SomeField = T.SomeField;
OPTION (MAXRECURSION 0)