oracle outer join query - oracle9i

I have 3 oracle tables. A joins to B and B joins to C. I want all records from A irerspective of whether a corresponding record exists in B or C. I wrote a query like this:
select a.name from a,b,c where a.a_id = b.b_id(+) and b.b_id = c.c_id(+)
This query does not seem right to me, particularly with the second join. What will exactly happen if there is a record in A but correspondingly nothing in B and C? Will it still fetch the record?
For some reason the above query returns same count of records as select a.name from a
So I am guessing that the query is right? Also is there a better way to rewrite the query?

I presume the better query can be
Select a.name from A a left join B b on a.a_id=b.b_id inner join C c on b.b_id=c.c_id
This should give the result as you have expected
http://rajanmaharjan.com.np

Related

Can I join a table to a coalesce field?

I’m trying to join three tables. Two of these, N1 and N2, share an ID (PK) but they differ in the number of rows, so I have written two coalesce statements to clean up the PK and a related field:
SELECT
(SELECT COALESCE(N1.ID_Year, N2.ID_Year)) AS ID_Year,
(SELECT COALESCE(N1.ReceiverUniqueID, N2.ReceiverUniqueID)) AS AllNetworkRUID,
[…]
FROM N1
FULL OUTER JOIN N2
ON N1.ID_Year_RU = N2.ID_Year_RU
What I need to do is now join the third table, N3, to the second COALESCE field, ie to ‘AllNetworkRUID’, something like:
JOIN N3
ON N3.ID = AllnetworkRUID .
I’ve not been able to work out how to make that happen, of if it’s even possible.
Any suggestions?

SQL Join :: Fetching records outside join condition

I have 2 tables A and B
A
B
The requirement is to join both tables using id column and along with that, if the fetched name value is having another record with a different id, that record should also be fetched. Like the below screenshot.
Output :
Requirements
Table B is in the size of TBs. single join of both tables will be
preferable
query needs to be executed on hive
I'm not familiar with HiveQL, but with regular SQL, you'll need to join table B to itself a second time as part of the query.
select
b_name.id, b_name.name
from
#table_A a
join #table_B b -- This table gets the "name" value for lookup
on (a.id=b.id)
join #table_B b_name -- This is the table you want to pull your "output" from
on (b.name=b_name.name)
This query essentially says that you need to find the value of the "name" column in table B, where there is a matching ID in table A, and then lookup all the rows with that name value in table B.
You can join the same table multiple times. So in the query below, b1 will give you all the names for the ids in A, and b2 is joined by name, to get you all the extra ids that are not in A.
select
b2.*
from
A
inner join B b1 on b1.id = A.id
inner join B b2 on b2.name = b1.name

Solr outer join / not join query

I may be asking too much but I want to do a left outer join between two cores
and get data from A only where B does not have related data.
Following is exactly my equivalent SQL query (for simplicity I have removed other conditions),
1. SELECT A.* FROM A AS A
WHERE A.ID NOT IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I understand that solr join is actually subquery, I need data from only A.
It would be very easy if the not was not there in where condition for sub query.
For example,
2. SELECT A.* FROM A AS A
WHERE A.ID IN (SELECT B.A_ID FROM B AS B WHERE B.STATUS_ID != 1)
I can have q={!join from=aId to=id fromIndex=b}(-statusId:1).
How can I do a nagete here, i.e. solr query for 1

Count the number of groups in HQL Query?

I have the following HQL query:
select c.device from Choice c
group by c.device
Now I want to count the number of groups in the result not the number of devices per group.
I tried:
select count(distinct c.device) from Choice c
group by c.device
but this give the number of distinct devices in each group. This is something like [2,3,4]. But I need 2+3+4.
How do I get the number of groups with HQL?
You would have to do a count of a count, which is not supported in HQL.
SQL
In SQL it would look something like this:
select count(innerQuery.counted)
from (select count(d.id) as counted
from Choice c inner join Device d
on c.device_id = d.id
group by d.id) as innerQuery
In the example above the outer query selects from a sub-query which returns the groups. The outer query then does a count on the generated counted column.
HQL
An alternative is to do a count and then get the size of the list.
Choice.executeQuery('select count(c.device) from Choice c group by c.device').size()
There's a performance penalty because the counting is done on the client-side.

Convert an Informix SQL query into ANSI-92 SQL?

What is the ANSI-92 equivalent of the following old Informix SQL query?
select *
from categories c,
orders o,
outer (employees e, person p)
where c.categoryid = o.categoryid
and p.personid = e.id
and o.employeeid = e.id
and o.orderid = 7742
and e.term_date is NULL
I cannot seem to figure out exactly what the "outer (table1, table2)" syntax means.
Mike Burdick's answer is, I believe, essentially correct — but I'm not completely sure of that. I'd be a little more comfortable with the comparison if the query were written as:
SELECT *
FROM Categories AS C
JOIN Orders AS O ON C.CategoryID = O.CategoryID
LEFT JOIN
(SELECT * FROM
FROM Employees AS E
JOIN Person AS P ON E.ID = P.PersonID
) AS E ON O.EmployeeID = E.ID
WHERE O.OrderID = 7742
AND E.Term_Data IS NULL
The OUTER (Employees e, Person p) in the original query is an inner-join sub-query that is outer-joined to the main query, which I've made clearer in my version by using the sub-query, but I think Mike's version is OK too. My concern is that Mike's version might more nearly equivalent to this (where the parentheses around employees e are optional in this scenario):
select *
from categories c,
orders o,
outer (employees e),
person p
where c.categoryid = o.categoryid
and p.personid = e.id
and o.employeeid = e.id
and o.orderid = 7742
and e.term_date is NULL
I'd need to think hard before coming up with data sets that could distinguish between the two queries (either the original query in the question and the slight rewrite above, or between Mike's answer and mine).
One other nagging doubt is that Informix's old-style non-standard OUTER join notation has a quirk that the standard notation simply does not support. The problem can occur when filtering on data in the subordinate table (RHS of a left outer join). The rows in the dominant table (in this case, tables — orders and categories) are preserved by Informix even when there was a match in the subordinate table that got filtered out. I think that it works OK this time because the filter condition that I'm concerned about, which is AND e.term_date IS NULL, uses IS NULL. You could run into the problem if the filter condition was AND e.term_date > MDY(7,4,2032) (and there was a row in employees that did not match the filter condition), but I think IS NULL is OK. The Informix behaviour is hard to explain, and hard to justify beyond "that is the way it works and it was documented to work thus in 1987±2 years, and backwards compatibility deals with the rest". It is only the Informix-only OUTER join notation that behaves weirdly. The ANSI-standard LEFT OUTER JOIN notation works according to the standard. But it does make conversion a challenge if this quirk affects your legacy code.
TL;DR Test the query results very carefully.
Something like:
select *
from categories c
Join orders o
on c.categoryid = o.categoryid
Left Join employees e
on o.employeeid = e.id
Join person p
on e.id = p.personid
where o.orderid = 7742
and e.term_data is null

Resources