Join SQL output of a N-M relationship table [duplicate] - sql

I want to join a table which has multiple rows and need to merge one of the column from all the rows into single column.
select a.parent_id,a.parent_name,concat(b.child_name) from parent a
join children b on (a.parent_id=b.parent_id);
This should return all the parent rows and each parent row should have all its children's.
i am thinking to group with parent_id but getting multiple records (one record per child). What logic i can implement here apart from grouping to get all child's for a parent in single row.

SELECT a.parent_id, a.parent_name, STRING_AGG(b.child_name, ',') as Children
FROM
Parent a
INNER JOIN children b
ON a.Id = b.ParentId
GROUP BY
a.parent_id
,a.parent_name

Related

Having trouble self joining a table with NULL values

I have a single table that has the columns: personID, name, parentID1 and parentID2
Essentially I am looking to use this table to find a childs parent(s)
Not every child has both parents, so these values are NULL in the table. I have tried using self joins but it just is not working out for me.
TABLE
Here is what the table should look like:
Need to use LEFT joins so each row is returned even if the there is no match in the right table
List Parents for Each Person Even if 1 Parent is NULL
SELECT A.[name] AS Child
,B.[name] AS Parent1
,C.[name] AS Parent2
FROM YourTable AS A
LEFT JOIN YourTable AS B
ON A.ParentID1 = B.PersonID
LEFT JOIN YourTable AS C
ON A.ParentID2 = C.PersonID
WHERE A.ParentID1 IS NOT NULL
OR A.ParentID2 IS NOT NULL

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

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

SQL Server hierarchy, selecting parent nodes for distinct IDs

Stuck on this hierarchy problem, so here goes.
I have the following hierarchy table, which has been truncated.
The hierarchy has been joined to another table on the 'codes' column, with the following sample result. Let's call this table1.
For each distinct ID in table1, I want to utilize the hierarchy to find a parent, if it exists.
For example, under the codes column, 18 is the parent of 19.
I would like to fold or group the 19 with 18 and eliminate that row.
The desired result is something like this.
This does sort of what I want, using the GetAncestor method:
SELECT A.OrgNode, A.Codes, A.ID, B.OrgNode, B.Codes, B.ID FROM table1 A INNER JOIN table1 B ON A.OrgNode.GetAncestor(1) = B.OrgNode WHERE A.ID = B.ID
This self-join, with the GetAncestor function, did what I wanted it to do, with the 'where' on the same ID.
SELECT A.OrgNode, A.Codes, A.ID, B.OrgNode, B.Codes, B.ID FROM table1 A INNER JOIN table1 B ON A.OrgNode.GetAncestor(1) = B.OrgNode WHERE A.ID = B.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.

Join with null values

I have two tables Parent and child table, child table contains foreign key constraint.
Now I want to get data from child table with join to parent table and also want the records where parent table id is null from child table.
select *
from child c
left join parent p on c.id = c.parent_id;