Oracle: Using results of one subquery inside another - oracle

select t1Joint2.c1,t1Joint3.c3 from
(select * from table1 where name = "some") t1,
(select t1.c1,t2.c2 from table2 t2 where t1.c1 = t2.c2) t1Joint2,
(select t1.c1,t3.c3 from table3 t3 where t1.c1 = t3.c3) t1Joint3,
;
The above query doesn't work in Oracle.
Any workaround?
Using Oracle 11g.

Why not write it as:
select t1.c1, t3.c3
from table1 t1
join table2 t2 on t1.c1 = t2.c2
join table3 t3 on t1.c1 = t3.c3
where name = "some"

with t1 as
(select * from table1 where name = 'some')
, t1Joint2 as
(select t1.c1,t2.c2 from t1, table2 t2 where t1.c1 = t2.c2)
, t1Joint3 as
(select t1.c1, t3.c3 from t1, table3 t3 where t1.c1 = t3.c3)
select t1Joint2.c1,t1Joint3.c3
from t1Joint2, t1Joint3
;

Related

Oracle Hierarchical queries: Translate START WITH ... CONNECT BY PRIOR into 'Recursive Subquery Factoring'

How would the following START WITH / CONNECT BY hierarchical query look like when translated into a RECURSIVE SUBQUERY FACTORING hierarchical query with WITH clause:
SELECT t1.id
FROM table1 t1, table2 t2
WHERE t1.version_id = t2.id
AND t1.baseline_date = TRIM (TO_DATE ('2015-05-26', 'yyyy-mm-dd'))
AND t2.entry_date = t1.baseline_date
START WITH t1.id IN (SELECT id
FROM table1
WHERE parent_id = 101015)
CONNECT BY PRIOR t1.id = t1.parent_id
ORDER SIBLINGS BY t1.child_index;
I think you want:
WITH rsqfc (id, child_index, baseline_date) AS (
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM table1 t1
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
WHERE t1.parent_id = 101015
UNION ALL
SELECT t1.id,
t1.child_index,
t1.baseline_date
FROM rsqfc r
INNER JOIN table1 t1
ON (r.id = t1.parent_id)
INNER JOIN table2 t2
ON ( t1.version_id = t2.id
AND t2.entry_date = t1.baseline_date )
)
SEARCH DEPTH FIRST BY child_index SET order_id
SELECT id
FROM rsqfc
WHERE baseline_date = DATE '2015-05-26';
However, without sample data it is difficult to be sure.

Oracle: Convert rows into columns for output of joined table queries

SELECT ABC.ID, ABC.URL1, ABC.URL2,
DECODE(ABC.TEXT,'URL3' ,ABC.URL),
DECODE(ABC.TEXT,'URL4',ABC.URL),
DECODE(ABC.TEXT,'URL5',ABC.URL),
DECODE(ABC.TEXT,'URL6',ABC.URL),
DECODE(ABC.TEXT,'URL7',ABC.URL),
DECODE(ABC.TEXT,'URL8',ABC.URL),
DECODE(ABC.TEXT,'URL9',ABC.URL)
FROM (SELECT * FROM (SELECT t1.ID, t2.URL1, t2.URL2,
t4.TEXT AS TEXT, t3.URL AS URL
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id=t2.id2
LEFT JOIN table4 t3 ON t3.id=t2.id2
LEFT JOIN table4 t4 ON t3.id3=t4.id
WHERE t1.id='VALUE'))ABC;
I want the output as below:
and values for those urls in respective columnst1
Below code worked for me:
SELECT DEF.ID, DEF.URL1, DEF.URL2,
MAX(DECODE(ABC.TEXT,'URL3' ,ABC.URL)) URL3,
MAX(DECODE(ABC.TEXT,'URL4',ABC.URL)) URL4,
MAX(DECODE(ABC.TEXT,'URL5',ABC.URL)) URL5,
MAX(DECODE(ABC.TEXT,'URL6',ABC.URL)) URL6,
MAX(DECODE(ABC.TEXT,'URL7',ABC.URL)) URL7,
MAX(DECODE(ABC.TEXT,'URL8',ABC.URL)) URL8,
MAX(DECODE(ABC.TEXT,'URL9',ABC.URL)) URL9
FROM (SELECT * FROM (SELECT t1.ID, t2.URL1, t2.URL2,
t4.TEXT AS TEXT, t3.URL AS URL
FROM table1 t1
LEFT JOIN table2 t2 ON t1.id=t2.id2
LEFT JOIN table4 t3 ON t3.id=t2.id2
LEFT JOIN table4 t4 ON t3.id3=t4.id
WHERE t1.id='VALUE')ABC) DEF
GROUP BY DEF.ID, DEF.URL1, DEF.URL2;

Union and With Clause SubQuery Oracle

I need immediate help in solving my problem before using in production:
Am using below SQL in Oracle11g
With sub_query1 as (select t1.column1, t2.column2, t1.id from table1 t1
inner join table2 t2 on t1.id = t2.id )
SELECT t3.column3 s1.* FROM sub_query1 s1
INNER JOIN table3 t3 ON t3.id=s1.id
union
SELECT t4.column4 s1.* FROM sub_query1 s1
INNER JOIN table4 t4 ON t4.id=s1.id;
This was working when I was using as is, but when add one subquery before SUB_QUERY1 it is not working, it is just returning 0 rows without any error:
With
main_sub_query as (select ta.id from tableA ta)
--second subquery
, sub_query1 as (select t1.column1, t2.column2, t1.id from table1 t1
inner join table2 t2 on t1.id = t2.id
inner join main_sub_query mq ON mq.id=t1.id
)
SELECT t3.column3 s1.* FROM sub_query1 s1
INNER JOIN table3 t3 ON t3.id=s1.id
union
SELECT t4.column4 s1.* FROM sub_query1 s1
INNER JOIN table4 t4 ON t4.id=s1.id;
When I remove the union and later part it is returning data, not sure what is the issue.

Want to change SQL query to LINQ

I want to change the below simple SQL query into LINQ , how do I change it ?
select * from table1 where isPaid = 'true' and Id in (select Id from table2 where EmployeeId = 12)
similar to this ?
from pa in db.PaymentAdvices
where pa.IsPaid == true
orderby pa.PaidDate descending
select pa;
Here code linq to sql:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12
select t1
Hope usefull!
if the field isPaid has datatype is Boolean:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid == true
select t1
if the field isPaid has datatype is String:
from t1 in table1
join t2 in table2 on t1.Id equals t2.Id
where t2.EmployeeId = 12 and t1.isPaid.Equals("true")
select t1

How to use join on three tables

I have three tables
Table1 Table2 and Table3.
Table1 having column ID.
Table2 having column names ID,Name.
Table three having column name Name.
Now i want to retrive ID from table1 which is there in Table2 by so that the name associated with ID in table to should be in Table3.
Table1.ID=Table2.ID(Table2.Name=Table3.Namw).
Without using IN operator.Only joins.
select table1.id, table2.name
from table1
join table2 on table2.id = table1.id
join table3 on table3.name = table2.name
select distinct t1.ID
from Table1 t1
,Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
;
or
select t1.ID
from Table1 t1
where exists (
select 1
from Table2 t2
,Table3 t3
where t1.ID = t2.ID
and t2.Name = t3.Name
);

Resources