Oracle SQL - left join + left outer join - sql

I am querying three tables. TABLE1 A and TABLE2 B have a one-to-one ratio on DEPTID. TABLE3 C, however, does not hold 0 values. I can successfully get COUNT to give me 0 values from TABLE3 C when doing a LEFT OUTER JOIN with TABLE1 A or TABLE2 B, but it gives me (null) instead of 0 when I join all three tables together. I need it to return 0 instead of (null). Any help is very much appreciated:
SELECT A.DEPTID, B.DEPT_NAME, SUM(C.HEAD_COUNT)
FROM TABLE1 A
LEFT JOIN TABLE2 B ON A.DEPTID = B.DEPTID
LEFT OUTER JOIN TABLE3 C ON A.POSITION_NUMBER = C.POSITION_NUMBER
GROUP BY A.DEPTID, B.DEPT_NAME
Here is what I am currently getting:
Dept 1: headcount 9
Dept 2: headcount 11
Dept 3: (null)

Use COALESCE() or NVL() to substitute 0 for NULL values:
SELECT A.DEPTID,
B.DEPT_NAME,
SUM(COALESCE( C.HEAD_COUNT, 0 ) )
FROM TABLE1 A
LEFT OUTER JOIN TABLE2 B
ON A.DEPTID = B.DEPTID
LEFT OUTER JOIN TABLE3 C
ON A.POSITION_NUMBER = C.POSITION_NUMBER
GROUP BY A.DEPTID,
B.DEPT_NAME

Related

Multiple SubQuery expressions : Hive

I am executing one hive query in my db in which i am joining 3 table(table1,table2,table3) and then comparing table1 year column with 3 other table(table4,table5,table6) using sub queries. I'm using below query.
select * from table1 A INNER JOIN table2 B ON A.id =b.id
inner JOIN table3 c ON A.id = c.id
and c.country ="India"
where (A.year)< (select year4 from table4)
and (A.year1)< (select year5 from table5 )
and (A.year1)< (select year5 from table5)
and (A.year1)< (select year6 from table6) limit 10;
but its giving me below error:
Error: Error while compiling statement: FAILED: SemanticException Line 0:-1 Unsupported SubQuery Expression
'year1': Only 1 SubQuery expression is supported.
someone please guide me how should i handle this.
EXPECTED OUTPUT
You need to put all tables in a join condition and then only you can compare year.
SELECT
*
FROM
TABLE1 A
INNER JOIN TABLE2 B ON A.ID =B.ID
INNER JOIN TABLE3 C ON A.ID = C.ID AND C.COUNTRY ="India"
INNER JOIN TABLE4 T4 ON T4.ID = A.ID AND A.YEAR < YEAR4
INNER JOIN TABLE5 T5 ON T5.ID = A.ID AND A.YEAR1 < YEAR5
INNER JOIN TABLE6 T6 ON T6.ID = A.ID AND A.YEAR1 < YEAR6
Pls check your SQL, year5 is compared twice.

Consolidating 5 Queries into One

I have two pieces of data that I have analysed and found five distinct ways in which the data is linked. Is it possible to combine these into one query? Combining queries like these is outside my skill set, but getting these queries into a singular one would be a huge help for what I am trying to accomplish.
Here are the queries (Actual Table and Column names have been replaced)
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table3 C ON B.Col4=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table5 E ON E.Col14=B.Col4 AND Col7='Value1'
LEFT JOIN Table6 F ON E.Col8=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 E ON E.Col10=B.Col4 AND Col7='Value2'
LEFT JOIN Table6 F ON E.Col11=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 C;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table7 F ON F.Col10=b.Col4 AND Col7='Value3'
LEFT JOIN Table3 C ON F.Col11=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
SELECT a.Col1, d.Col2 FROM Table1 A
RIGHT JOIN Table2 B ON A.Col1=B.Col3
LEFT JOIN Table8 E ON E.Col12=B.Col4 AND Col7='Value4'
LEFT JOIN Table6 F ON E.Col13=F.Col9
LEFT JOIN Table3 C ON F.Col9=C.Col5
LEFT JOIN Table4 D ON D.Col2=C.Col6 ;
The result of the queries should give data like below. For any value in Col1 there could be multiple values in Col2, however, for each Col1/Col2 pairing, only 1 set of the queries above creates the link between the two entities.
Col1 | Col2
-----------
1 | A
2 | B
2 | C
3 | D
4 | A
Thank you for any assistance. Let me know if have any questions on the queries or results.
Note - These queries are being executed against an Oracle database.
Use:
SELECT a.Col1, d.Col2
FROM Table2 B
LEFT JOIN Table1 A ON A.Col1=B.Col3
LEFT JOIN Table5 H ON H.Col14=B.Col4 AND Col7='Value1'
LEFT JOIN Table7 E ON (E.Col10=B.Col4 AND Col7='Value2') OR (F.Col10=b.Col4 AND Col7='Value3')
LEFT JOIN Table6 F ON (E.Col13=F.Col9) OR (E.Col8=F.Col9) OR (E.Col11=F.Col9) OR (H.Col8=F.Col9)
LEFT JOIN Table3 C ON (B.Col4=C.Col5) OR (F.Col9=C.Col5) OR (F.Col11=C.Col5)
LEFT JOIN Table4 D ON D.Col2=C.Col6;
or a UNION operator for your select statements.

Covert left join to simple join

My LEFT JOIN subquery is taking longer. How do I covert it to JOIN?
SELECT * FROM "TABLE_A"
LEFT OUTER JOIN TABLE_B ON TABLE_A.mainid = TABLE_B.secondID
LEFT JOIN (
SELECT secondID
FROM TABLE_C
WHERE thirdID = 21
) Z ON TABLE_A.mainid = Z.secondID
WHERE "TABLE_A"."TRUEID" = 9
AND ((TABLE_A.USERNO IN (23))
First, write the query more concisely:
SELECT *
FROM "TABLE_A" a LEFT JOIN
TABLE_B b
ON a.mainid = b.secondID LEFT JOIN
TABLE_C c
ON a.mainid = c.secondID AND c.thirdID = 21
WHERE a."TRUEID" = 9 AND a.USERNO IN (23);
Then consider the indexes:
Table_A(TRUEID, USERNO, mainid)
Table_B(secondID)
Table_C(secondID, thirdID)

Compare 2 fields in different rows of the same table

I want to return results from a join where the birth_date of two+ records are the same, but the person_id's are not equal. I am using Oracle. So if I got 4 results where rows 1 & 2 have the same birth_date and different person_id, those rows would be returned. Where rows 3 & 4 have the same birth_date and same person_id, those rows would not be returned. I get results, but I want to filter our results where the birth_date of rows are equal, but the person_id is <>.
select t3.field1, t6.field2, t6.field3, t3.field4, t3.field5
from table1 t1
inner join table2 t2 on t1.#matching = t2.#matching
inner join table3 t3 on t3.#matching = t1.#matching
inner join table4 t4 on t4.#matching = t1.#matching
inner join table5 t5 on t5.#matching = t4.#matching
inner join table6 t6 on t6.#matching = t3.#matching
where t1.#requirement = 'xxx'
and t2.#requirement = 'xxx'
and t2.#requirement is null
and t4.#requirement = 'xxx'
and t5.#requirement = 'xxx'
and t1.#requirement ='xxx'
and t5.#requirement is null
order by t1.#field ASC;
SELECT a.birth_date, a.id, b.id
FROM some_table a, some_table b
WHERE a.birth_date = b.birth_date
AND a.id < b.id
Note the usage of < instead of the intuitive !=. This is done to prevent the same combination returning in different orders (e.g. (1,2) and (2,1)).
Have you tried to group your data by person_id.. This will put all the records with the same person_id into 1 record
The query would look something like this
SELECT a.birth_date, a.id, b.id
FROM some_table a, some_table b
WHERE a.birth_date = b.birth_date
AND a.id < b.id
GROUP BY a.id

Syntax for multiple joins in sql

Working on Oracle: I am attempting to do an inner self join, with a where clause, then take that result and do a left outer join on it:
(select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id
Somehow I am syntactically challenged and can't make this work. Can't seem to find the right syntax anywhere.
Just the put the where clause at the end. The database will get it right:
select *
from table1 A
inner join table1 B on A.id = B.id
left join table2 D on D.id = A.id
where A.id is not null
In this case, we can take advantage of the logical transitive property for your id column joins and where clause.
Your second join needs to be joined to a query add a select * from at the beginning
select * from (select * from table1 A
inner join
select * from table1 B
on A.id = B.id
where
A.id is not null and B.id is not null) C
left outer join
select * from table2 D
on C.id = D.id