SQL - How to query for same table, same column, different data? - sql

I am trying to create a short hierarchy query in SQL.
Tables & Columns used (Schema)
In table table_A, I have employee information in columns: Name, ID (HR-type data).
In table table_B, I have relationship data in columns RelType, ChildID, ParentID.
Table_B's columnsChildID and ParentID are populated with table_A's ID data.
Example
If my table_A, ID is 1 and my boss's ID is 2, then in table_B, our relationship would be expressed by showing the ChildID as 1 and the ParentID as 2.
How do I write the query to pull in the Child HR data and the Parent HR data on the same row?
I can pull in all the Child HR data I need with below query, but cannot figure out how to join or alias in a way that will pull in table_A's column ID for two different values on the same row.
SELECT
table_a.name
table_a.id
table_b.reltype
FROM table_a
LEFT JOIN table_b ON table_a.id=table_b.childid
This obviously does not include any data for the Parent info. For every ID in table_A, if that ID exists in table_B, it will be in both Child and Parent ID columns. I want to capture all the "children" and include their "parent" (there will only be one for this purpose) on the same row.
EDIT: Below answer worked, needed two joins with aliases. Thx.

You seem to be looking for two joins on table_a, like so:
select
b.reltype
b.childid,
ac.name childname,
b.parentid,
ap.name parentname
from table_b b
inner join table_a ac on ac.id = b.childid
inner join table_a ap on ap.id = b.parentid

Related

Select from table where another value doesn't exist in any of the joined tables

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

How to create view with select two relation table one to one, and fill all null value with parent value - postgreSQL

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)

SQL - Need to conditionally join based on value in TableB column

I need to know the rows in TABLE A that have join records in TABLE B based a column value in TABLE B, but I also need to return rows in which a row in TABLE A has no match in TABLE B.
It seems like I need a LEFT JOIN and a LEFT OUTER JOIN, so I'm not sure what to do there. I understand how to do each, but don't understand how to do them together.
The schema looks like:
TABLE_A
pk
TABLE_B
pk
a_fk
some_value
I need the joined rows where Table_A has no join record in Table_B OR Table_A has a join record row in Table_B (it can have many) in which some_value does not equal "thisValue"
Thanks.
A Left join is a left outer join. Outer joins preserve one of the tables which is what you are after so good guess.
SELECT *
FROM Table A
LEFT JOIN Table B
ON TableA.Column = TableB.Column
AND B.SomeValue <> 'ThisValue'
All of the rows with a match will have the B information populated all of those without will have nulls in the B data

Join 2 tables by ID who from first table

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

Select records whose id does not appear in multiple tables

I have 3 tables. Two tables (par1, par1) both refer (child_id) to an id in the 3rd table (child).
I like to find orphans in the 3rd table, i.e. records that are not referenced by either of the other two tables.
If I had only once referencing table, I could write:
SELECT * FROM child WHERE id NOT IN (SELECT child_id FROM par1)
But how do I solve this for two referencing tables?
I'm using sqlite.
SELECT *
FROM child
WHERE id NOT IN (SELECT child_id FROM par1) AND
id NOT IN (SELECT child_id FROM par2)
An alternative is to use LEFT OUTER JOIN:
SELECT child.*
FROM child LEFT OUTER JOIN par1 ON (child.id = par1.child_id)
LEFT OUTER JOIN par2 ON (child.id = par2.child_id)
WHERE par1.child_id IS NULL AND par2.child_id IS NULL
Executing subqueries may or may not find the desired optimization paths in the SQLite index decision engine.