SELECT *
FROM (SELECT ROWNUM rnum,
query.*
FROM (WITH myQuery AS(
SELECT column_b
FROM table_a a
WHERE a.column_a = 1234)
SELECT b.column_e AS some_column
FROM table_b b,
table_c c,
table_a a
LEFT JOIN table_d d ON c.column_c = d.column_d
JOIN myQuery mq ON a.column_b = mq.column_b
WHERE b.column_b = a.column_b) query)
WHERE rnum > 0
Don't mix ANSI-88 and ANSI-92 JOIN syntax, pick one or the other. Here's your query using ANSI-92 syntax:
WITH myQuery AS (
SELECT column_b
FROM table_a a
WHERE a.column_a = 1234)
SELECT x.*
FROM (SELECT b.column_e AS some_column,
ROWNUM 'rnum'
FROM table_b b
JOIN TABLE_A a ON a.column_b = b.column_b
JOIN myQuery mq ON mq.column_b = a.column_b
JOIN table_c c ON c.? = ?? --need join criteria here
LEFT JOIN table_d d ON c.column_c = d.column_d) x
WHERE x.rnum > 0
Your example lacks what TABLE_C joins on to - hence the ? and ??
I didn't know that WITH clauses can be defined in subqueries - I was sure I'd encountered an error in the past when attempting it in 10g.
Related
select t.col1,b.somecolumn t.col2,a.col1,VQ.a,VQ.b,VQ.e,VQ.d,VQ.f,
(select t.status as a, p.id as b,p.permit as c, p.des as d, p.error_code as e, p.cause as f
from table_A t
inner join table_B p on t.a = p.a
where p.c = 'license' and t.status = 'Fail') as VQ
from table_A t
join table_C a on t.col1 = a.asset_id
join table_B b on t.somecolumn = b.somecolumn ;
When I execute the above code, I encounter the error
SQL Error [42601]: ERROR: navigation on column "vq" is not allowed as it is not SUPER type
I am trying to do a select inside select.
It looks like you want to return multiple columns from a correlated subquery. If so, you can do so with a lateral join:
select t.col1, b.somecolumn t.col2, a.col1, vq.*
from table_A t join
table_C a
on t.col1 = a.asset_id join
table_B b
on t.somecolumn = b.somecolumn left join lateral
(select t.status as a, p.id as b,p.permit as c, p.des as d, p.error_code as e, p.cause as f
from table_A t join
table_B p on t.a = p.a
where p.c = 'license' and t.status = 'Fail'
) vq
on 1=1;
I am creating a giant SQL query:
Select * from (With tab1 AS ( Select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
UNION
select a, b from cde)
Select tab1.*, tab3.* from
tab1
LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
...
LEFT OUTER JOIN tabz on tab1.a = tabz.a) A
Now using the above as 1 table I need to create another long SQL to calculate percentages and other things with other tables tables.
Say above table is A
then
Select bbb.a,bbb.b from bbb
JOIN A ON bbb.a = A.a and then name it as B
And then finally join A LEFT OUTER JOIN B.
It is a massive Query and I know we can not have Nested WITH statement. Does anyone have any easy way to complete this? OR any suggestion?
I only need to accomplish this using oracle SQL queries.
I think you can rephrase your query as:
WITH
tab1 AS (select * from abc)
,tab2 AS (select * from cde)
,tab3 AS (select * from tab2)
.
.
.
,tabz AS (select a, b from xyz
UNION
select a, b from cde)
,a as (
Select tab1.*, tab3.*
from tab1
LEFT OUTER JOIN tab2 ON tab1.a = tab2.b
...
LEFT OUTER JOIN tabz on tab1.a = tabz.a
),
b as (
Select bbb.a,bbb.b from bbb JOIN A ON bbb.a = A.a
)
select *
from a
left join b on ...
Is it possible to access a table from a subquery?
Select d.table_c.*
from (with table_c as (select *
from table_a)
select *
from table_b
where table_a.id = table_b.id) as d
table_c is inside the subquery of d, I've tried to access it using d.table_c, but it doesn't seem to work.
You cannot use CTE as subquery. But you can write like below.
;WITH table_c
as
(SELECT * FROM table_a)
SELECT *
from table_b b
INNER JOIN table_c c on c.id = b.id
I have SELECT statement with LEFT JOIN and joined tables are sub-queries. And Oracle could not recognize alias of the first sub-query in the second one. It works for DB2 but does not work for Oracle.
How I can implement it or rewrite my query?
SELECT *
FROM
(SELECT E.C3 AS COLUMN3
, E.C4 AS COLUMN4
FROM TBL_1 D
, TBL_2 E
WHERE D.C6 = E.C6 ) B
LEFT JOIN TABLE
(SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'hello'
AND B.C3 = C.C8
UNION ALL
SELECT C.C1
FROM TBL_3 C
WHERE
C.C7 = 'world'
AND B.C4 = C.C8
) A
ON 1 = 1
Oracle error message:
ORA-00904: "B"."C3": invalid identifier
You can simplify this query to the following, removing the sub-queries:
Select A.Col1, B.Col2
From tbl_AJoin A
Left Join tbl_BJoin B On A.col1 = B.col1
You have a syntax error. This:
select * from (select col1 from tbl_Ajoin) A
left join table (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
should be this:
select * from (select col1 from tbl_Ajoin) A
left join (select col2 from tbl_Bjoin where A.col1 = tbl_Bjoin.col1) B
ON 1 = 1
or more specifically, this:
left join table (select
should not have the word table. It should be this:
left join (select
I have table A, B and C
I want to return all entries in table A that do not exist in table B and of that list do not exist in table C.
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
this gives me the first result of entries in A that are not in B. But now I want only those entries of this result that are also not in C.
I tried flavours of:
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
AND
where not exists (select 1 from table_C as c
where a.id = c.id)
But that isnt the correct logic. If there is a way to store the results from the first query and then select * from that result that are not existent in table C. But I'm not sure how to do that. I appreciate the help.
Try this:
select * from (
select a.*, b.id as b_id, c.id as c_id
from table_A as a
left outer join table_B as b on a.id = b.id
left outer join table_C as c on c.id = a.id
) T
where b_id is null
and c_id is null
Another implementation is this:
select a1.*
from table_A as a1
inner join (
select a.id from table_A
except
select b.id from table_B
except
select c.id from table_c
) as a2 on a1.id = a2.id
Note the restrictions on the form of the sub-query as described here. The second implementation, by most succinctly and clearly describing the desired operation to SQL Server, is likely to be the most efficient.
You have two WHERE clauses in (the external part of) your second query. That is not valid SQL. If you remove it, it should work as expected:
select * from table_A as a
where not exists (select 1 from table_B as b
where a.id = b.id)
AND
not exists (select 1 from table_C as c -- WHERE removed
where a.id = c.id) ;
Tested in SQL-Fiddle (thnx #Alexander)
how about using LEFT JOIN
SELECT a.*
FROM TableA a
LEFT JOIN TableB b
ON a.ID = b.ID
LEFT JOIN TableC c
ON a.ID = c.ID
WHERE b.ID IS NULL AND
c.ID IS NULL
SQLFiddle Demo
One more option with NOT EXISTS operator
SELECT *
FROM dbo.test71 a
WHERE NOT EXISTS(
SELECT 1
FROM (SELECT b.ID
FROM dbo.test72 b
UNION ALL
SELECT c.ID
FROM dbo.test73 c) x
WHERE a.ID = x.ID
)
Demo on SQLFiddle
Option from #ypercube.Thank for the present;)
SELECT *
FROM dbo.test71 a
WHERE NOT EXISTS(
SELECT 1
FROM dbo.test72 b
WHERE a.ID = b.ID
UNION ALL
SELECT 1
FROM dbo.test73 c
WHERE a.ID = c.ID
);
Demo on SQLFiddle
I do not like "not exists" but if for some reason it seems to be more logical to you; then you can use a alias for your first query. Subsequently, you can re apply another "not exists" clause. Something like:
SELECT * FROM
( select * from tableA as a
where not exists (select 1 from tableB as b
where a.id = b.id) )
AS A_NOT_IN_B
WHERE NOT EXISTS (
SELECT 1 FROM tableC as c
WHERE c.id = A_NOT_IN_B.id
)