All,
I am trying to pair two or more records linked by a common Grandparent record.
At the time I run my query I will only know one of the records in the tuple / pair. Therefore I need to use this child record to navigate to the Grandparent record.
From the grandparent record I will then need to find all other Grandchildren records.
Here is a rather crude drawing of the structure
So far in my query I can find the Grandparent record but I do not know what to do from that point to find all other grand children
SELECT * FROM TABLE A
WHERE CREATED_DATE = sysdate
JOIN TABLE B
ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C
//Grandparent table
ON TABLE B.parent_row = TABLE C.row_id
If there is a better way to do this? My only main filter in this query is that I am searching for records created today in Table A.
Thanks
First, your query is invalid. The where clause comes after the joins:
SELECT *
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
WHERE CREATED_DATE = sysdate;
Secondly, you didn't filter by the grand-child you apparently have already. In this query, I use the YOUR_ID variable to represent it:
SELECT *
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
WHERE
CREATED_DATE = sysdate
AND TABLE A.row_id = YOUR_ID;
Thirdly, now that we retrieved the grand parents (table C.row_id), we may follow the same logic to join over again with table B (BB) and A (AA) to get all grand children:
SELECT TABLE AA.row_id /* ids of all grand children. YOUR_ID should be included */
FROM TABLE A
JOIN TABLE B ON TABLE A.parent_row = TABLE B.row_id
JOIN TABLE C ON TABLE B.parent_row = TABLE C.row_id
/* join over on table B then table A to get all grand children */
JOIN TABLE BB ON TABLE BB.parent_row = TABLE C.row_id
JOIN TABLE AA ON TABLE AA.parent_row = TABLE BB.row_id
WHERE
CREATED_DATE = sysdate
AND TABLE A.row_id = YOUR_ID;
Related
I am trying to retrieve the IDs from a table in Oracle only IF another column value doesn't exist in any of the joined tables. Let me give you an example:
example
As you can see in the sketch, Table A is joined to tables B via the ID. I would like to get the IDs from Table A only if all statuses in any joined Table B DO NOT contain the value 2.
Here is my SQL statement:
SELECT ID FROM TABLE A
LEFT JOIN TABLE B
ON A.ID = B.REF_ID
WHERE B.STATUS NOT IN (2)
Unfortunately, I still get all IDs (which makes sense) and am not able to come up with a method to retrieve only the IDs without a certain value in the Status column of a joined table. Hence, I would only like to get ID 1, since all joined tables do not contain the value 2 in their Status.
Many thanks for any inputs.
Use aggregation
SELECT ID
FROM TABLE A LEFT JOIN
TABLE B
ON A.ID = B.REF_ID
GROUP BY A.ID
HAVING SUM(CASE WHEN B.STATUS IN (2) THEN 1 ELSE 0 END) = 0;
Or, simply use NOT EXISTS:
SELECT A.ID
FROM TABLE A
WHERE NOT EXISTS (SELECT 1
FROM B
WHERE A.ID = B.REF_ID AND B.STATUS IN (2)
);
I have 2 tables, structured like this:
table A table B
- id - id
- name - name
- parent
- fk_b
so in table A there are parent and fk_b. for fk_b it can be null.
so if in table A (child) the fk_b is null will refer to table A (parent), for example on of the row in the table A (parent) has fk_b.
Note:
This parent contains the id of table A
my question is, how can we select table A join table B. But every table A with null fk_b will retrieve fk_b from its parent?
Thank you
You can self-join the table to retrieve the parent row, and implement conditional logic in the on clause of the join on tableb, like so:
select a.*, b.name
from tablea a
left join tablea p on p.id = a.parent
left join tableb b on b.id = coalesce(a.fk_b, p.fk_b)
I have 2 tables, One with Auto Increment ID and the other with a column ID but no auto increment. I want IDs from the first table will be in the column ID of the second table.
I know Inner Join/ left/ right but it seems this is not what I really want.
select *
from table_a a
inner join table_b b on a.auto_increment_id = b.column_id
I have two tables A (primary key - unit_id) and B (primary key - unit_id)
I have a value (eg :4 ) in table A and has a unit_id.
I have 4 rows in table B with the same unit ID
I have to write a SQL query to check whether the value in table A matches with the count (rows) in table B with the same unit_id
You can just use inner join and you will see how many values from table A are in table B :
Select a.unit_id from Table1 a inner join Table2 b on a.unit_id = b.unit_id
Im assumin that is what you need , because as #StanislavL pointed out, you cant have more then one unique unit_id in each table.
I'm just curious, if i have table a and table b.
I write query 1:
SELECT * FROM table a INNER JOIN table b ON table a.id = table b.id
I write query 2:
SELECT * FROM table b INNER JOIN table a ON table b.id = table a. id
What is the difference both of above query?
Thank you
When using INNER JOIN , there is no difference in resultset returned except in order of columns when SELECT * is used i.e. columns are not explicitly mentioned.
SELECT *
FROM table a
INNER JOIN table b
ON table a.id = table b.id
returns columns from tableA followed by columns from tableB
SELECT *
FROM table b
INNER JOIN table a
ON table b.id = table a. id
returns columns from tableB followed by columns from tableA
The second table matches data with the first one.
So it is better to put smaller table on the second place.