How to use join on three tables - oracle

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
);

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;

Comparing two columns one of which is null

I have three tables as follow
TABLE1
ID VALUE
1 NULL
TABLE2
ID VALUE
1 1
TABLE3
ID VALUE
1 10
I'm trying to do the following:
Compare the two fields (ID) in tables 1 & 2 if one is null return null then return value from TABLE3.
It seems you are looking for something like this:
select table3.id, table3.value
from table1 join table2 on table1.id = table2.id
join table3 on table1.id = table3.id
where table1.value is null or table2.value is null
If this isn't what you had in mind, you need to give more details about your requirement.
Try this query:
SELECT t1.id,
(CASE WHEN t1.value IS NULL OR t2.value IS NULL
THEN t3.value
END) VALUE
FROM table_1 t1
JOIN table_2 t2
ON t1.id = t2.id
JOIN table_3 t3
ON t1.id = t3.id

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

Oracle: Using results of one subquery inside another

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
;

Resources