WebSQL selecting child + parent - sql

I'm trying to export a list of categories with it's childs.
the designer of the DB made it so that a parent category can have the same ID of a child
so the query should return a list of:
Parent items
Child items with the parent name
I'm trying to do this in 1 Query, So I did the following:
SELECT DISTINCT c.*, pc.name as parentName
FROM Category as c
INNER JOIN Category as pc on c.parent_id = pc.id
WHERE c.building_id = 1
AND (
c.parent_id = -1
OR (
c.usingtemplate = 'true'
AND
pc.parent_id = -1
)
);
but the parents aren't in the list
I have a sql fiddle here: http://sqlfiddle.com/#!7/75dd9/83

Doing a LEFT JOIN did the trick
SELECT DISTINCT c.*, pc.name as parentName
FROM Category as c
LEFT JOIN Category as pc on c.parent_id = pc.id
WHERE c.building_id = 1
AND (
c.parent_id = -1
OR (
c.usingtemplate = 'true'
AND
pc.parent_id = -1
)
);

Related

SQL query where has children with conditions

Let's say I have:
a parents table with the following columns: (id, name)
a children attributes table with the following columns: (id, child_id, parent_id, attribute, attribute_value)
Now I want to filter any parent id's that has at least a child with both:
attribute => intelligence of 5
attribute => health of 4
Either one child with intelligence of 5 and health of 4, or one child has intelligence of 5 and another child has health of 4.
How would you query that, in PostgreSQL? Thank you
You can just do the intersection of
parents that have children with intelligence 5
parents that have children with health 4
(SELECT parent_id
FROM tab
WHERE attribute = 'intelligence'
AND attribute_value = 5 )
INTERSECT
(SELECT parent_id
FROM tab
WHERE attribute = 'health'
AND attribute_value = 4 )
If you only wants parents info:
SELECT
DISTINCT parents.id, parents.name
FROM
parents
LEFT JOIN attributes ON parents.id = attributes.parent_id
WHERE
(attribute = 'intelligence' AND attribute_value = 5)
OR (attribute = 'health' AND attribute_value = 4)
First we need to join the tables -- like this
select p.id as p_id, p.name as parent_name,
k.* -- we won't need this in later versions
from parent p
join kidatt k on p.id = k.parent_id
now we have two attributes we care about -- let make a query that shows those
select p.id as p_id, p.name as parent_name,
case when k.attribute = 'intelligence' and k.attribute_value = 5 then 1 else 0 end as has_a1,
case when k.attribute = 'health' and k.attribute_value = 4 then 1 else 0 end as has_a2
from parent p
join kidatt k on p.id = k.parent_id
we now have a query with a 1 in a row for those that have each of these
now we group by the parent.
select p.id as p_id, p.name as parent_name,
SUM(case when k.attribute = 'intelligence' and k.attribute_value = 5 then 1 else 0 end) as has_a1,
SUM(case when k.attribute = 'health' and k.attribute_value = 4 then 1 else 0 end) as has_a2
from parent p
join kidatt k on p.id = k.parent_id
group by p.id, p.name
now we have a query where a1 and a2 are greater than 0 if one or more child has it.
Now just select the results
select *
from (
select p.id as p_id, p.name as parent_name,
SUM(case when k.attribute = 'intelligence' and k.attribute_value = 5 then 1 else 0 end) as has_a1,
SUM(case when k.attribute = 'health' and k.attribute_value = 4 then 1 else 0 end) as has_a2
from parent p
join kidatt k on p.id = k.parent_id
group by p.id, p.name
)
where has_a1 > 0 and has_a2 > 0
note -- I did not write this query to be the best way to solve this problem -- instead I wrote it in a way to show you how to "think" in SQL and solve the problem with a series of steps.
I'd have to test to be sure, but I expect this would be the fastest way to do this query (depends on data and indexes etc.)
select distinct p.id as p_id, p.name as parent_name,
from parent p
join kidatt k on p.id = k.parent_id
where k.attribute = 'intelligence' and k.attribute_value = 5
intersect
select distinct p.id as p_id, p.name as parent_name,
from parent p
join kidatt k on p.id = k.parent_id
where k.attribute = 'health' and k.attribute_value = 4

Checking against a table before query?

In the following query, how could I do a sub query to find all categories in exp_categories that have a parent_id AND all categories that are a parent, but don't have any children, then use this instead of the AND c.parent_id != '0'?
SELECT c.cat_url_title
FROM exp_channel_titles as t
LEFT JOIN exp_category_posts AS cp ON cp.entry_id = t.entry_id
LEFT JOIN exp_categories AS c ON cp.cat_id = c.cat_id
WHERE t.url_title = 'hummingbird'
AND c.parent_id != '0'
AND c.cat_url_title != 'latest-work'
AND c.cat_url_title != 'best-selling-images'
LIMIT 1
Something like this?
SELECT c.cat_url_title
FROM exp_channel_titles as t
LEFT JOIN exp_category_posts AS cp ON cp.entry_id = t.entry_id
LEFT JOIN exp_categories AS c ON cp.cat_id = c.cat_id
WHERE t.url_title = 'hummingbird'
AND c.parent_id in (
select id from exp_categories where parent_id is not null
union
select id from exp_categories where id not in (
select parent_id from exp_categories
)
)
AND c.cat_url_title != 'latest-work'
AND c.cat_url_title != 'best-selling-images'
LIMIT 1

Join between Parent and Child table

I've 2 tables: Parent and Child with following data
Now when I execute the following queries:
delete from Parent where Id in(2,3,4)
delete from Child
The only record is left Parent table
Now when I execute the following query I don't get any records
select p.Id AS [ParentId],p.Name AS [ParentName], c.Id, c.Name from Parent p
Left join Child c on p.Id = c.ParentId
where p.IsActive = 1 and c.IsActive = 1
And when I remove and c.IsActive = 1 from above query I get the record in Package table, but I want to apply both the active checks. How to achieve this?
Your where clause turns your left join into an inner join. Use
select p.Id AS [ParentId],p.Name AS [ParentName], c.Id, c.Name
from Parent p
Left join Child c on p.Id = c.ParentId
and c.IsActive = 1
where p.IsActive = 1
All filters of the left joined table need to be in the on clause.
Another solution is to treat both cases in the where clause condition (both when c.IsActive is null - missing row in child table - or is 1)
where p.IsActive = 1 and COALESCE(c.IsActive, 1) = 1

how to optimized without union clauses?

Is it possible to write below query without a union clause.
select ProductId,ImageName,ImageType, ROW_NUMBER() over (order by ProductId desc) RowId
from
(
select p.id ProductId ,p.pic_image ImageName,'pic_image' ImageType
from product p
left outer join iimages_edited pe on p.id = pe.[id]
where isnull(p.pic_image,'') <> '' and isnull(pe.pic_image,0)=0
union
select p.id ProductId,p.pic_bimage ImageName,'pic_bimage' ImageType
from product p
left outer join iimages_edited pe on p.id = pe.[id]
where isnull(p.pic_bimage,'') <> '' and isnull(pe.pic_bimage,0)=0
union
select p.id ProductId,p.pic_limage ImageName,'pic_limage' ImageType
from product p
left outer join iimages_edited pe on p.id = pe.[id]
where isnull(p.pic_limage,'') <> '' and isnull(pe.pic_limage,0)=0
union
select p.id ProductId,p.pic_blimage ImageName,'pic_blimage' ImageType
from product p
left outer join iimages_edited pe on p.id = pe.[id]
where isnull(p.pic_blimage,'') <> '' and isnull(pe.pic_blimage,0)=0
union
select p.id ProductId,p.pic_cimage ImageName,'pic_cimage' ImageType
from product p
left outer join iimages_edited pe on p.id = pe.[id]
where isnull(p.pic_cimage,'') <> '' and isnull(pe.pic_cimage,0)=0
)t
Above query has same table but different where condition, It is
possible to do it in a single query ?
Any help will be much appreciated !
Thanks in advance
It seems that you are repeating the same join and filters with differents columns each time. You can convert them to rows using UNPIVOT, on each table, before the join :
select pe.ProductId, p.ProductId, p.ImageName, p.ImageType, ROW_NUMBER()
over (order by p.ProductId desc) RowId
from (
select id as ProductId, ImageType, ImageName
from product
unpivot (
ImageType for ImageName
in (pic_image, pic_bimage, pic_limage, pic_blimage, pic_cimage)
) t
) as p
left outer join (
select id as ProductId, ImageType, ImageName
from iimages_edited
unpivot (
ImageType for ImageName
in (pic_image, pic_bimage, pic_limage, pic_blimage, pic_cimage)
) t
) as pe
on p.ImageType = pe.ImageType
and p.ProductId = pe.ProductId
where pe.ProductId is null
UNPIVOT filters null values, so ISNULL are probably not necessary.

Finding a Top Level Parent in SQL

I have got two tables as following
Table Person
Id Name
1 A
2 B
3 C
4 D
5 E
Table RelationHierarchy
ParentId ChildId
2 1
3 2
4 3
This will form a tree like structure
D
|
C
|
B
|
A
ParentId and ChildId are foreign keys of Id column of Person Table
I need to write SQL that Can fetch me Top Level Parent i-e Root. Can anyone suggest any SQL that can help me accomplish this
You can use recursive CTE to achieve that:
DECLARE #childID INT
SET #childID = 1 --chield to search
;WITH RCTE AS
(
SELECT *, 1 AS Lvl FROM RelationHierarchy
WHERE ChildID = #childID
UNION ALL
SELECT rh.*, Lvl+1 AS Lvl FROM dbo.RelationHierarchy rh
INNER JOIN RCTE rc ON rh.CHildId = rc.ParentId
)
SELECT TOP 1 id, Name
FROM RCTE r
inner JOIN dbo.Person p ON p.id = r.ParentId
ORDER BY lvl DESC
SQLFiddle DEMO
EDIT - for updated request for top level parents for all children:
;WITH RCTE AS
(
SELECT ParentId, ChildId, 1 AS Lvl FROM RelationHierarchy
UNION ALL
SELECT rh.ParentId, rc.ChildId, Lvl+1 AS Lvl
FROM dbo.RelationHierarchy rh
INNER JOIN RCTE rc ON rh.ChildId = rc.ParentId
)
,CTE_RN AS
(
SELECT *, ROW_NUMBER() OVER (PARTITION BY r.ChildID ORDER BY r.Lvl DESC) RN
FROM RCTE r
)
SELECT r.ChildId, pc.Name AS ChildName, r.ParentId, pp.Name AS ParentName
FROM CTE_RN r
INNER JOIN dbo.Person pp ON pp.id = r.ParentId
INNER JOIN dbo.Person pc ON pc.id = r.ChildId
WHERE RN =1
SQLFiddle DEMO
EDIT2 - to get all persons change JOINS a bit at the end:
SELECT pc.Id AS ChildID, pc.Name AS ChildName, r.ParentId, pp.Name AS ParentName
FROM dbo.Person pc
LEFT JOIN CTE_RN r ON pc.id = r.CHildId AND RN =1
LEFT JOIN dbo.Person pp ON pp.id = r.ParentId
SQLFiddle DEMo
I've used this pattern to associate items in a hierarchy with the item's root node.
Essentially recursing the hierarchies maintaining the values of the root node as additional columns appended to each row. Hope this helps.
with allRows as (
select ItemId, ItemName, ItemId [RootId],ItemName [RootName]
from parentChildTable
where ParentItemId is null
union all
select a1.ItemId,a1.ItemName,a2.[RootId],a2.[RootName]
from parentChildTable a1
join allRows a2 on a2.ItemId = a1.ParentItemId
)
select * from allRows
To find all top-level parents, use a query like:
select p.Name
from Person p
where not exists
(select null
from RelationHierarchy r
where r.ChildId = p.Id)
SQLFiddle here.
To find the top-level parent of a specific child, use:
with cte as
(select t.ParentId TopParent, t.ChildId
from RelationHierarchy t
left join RelationHierarchy p on p.ChildId = t.ParentId
where p.ChildId is null
union all
select t.TopParent TopParent, c.ChildId
from cte t
join RelationHierarchy c on t.ChildId = c.ParentId)
select p.name
from cte h
join Person p on h.TopParent = p.Id
where h.ChildId=3 /*or whichever child is required*/
SQLFiddle here.
Try this.
The recursive CTE will find the person and walk up the hierarchy until it finds no parent.
-- This CTE will find the ancestors along with a measure of how far up
-- the hierarchy each ancestor is from the selected person.
with ancestor as (
select ParentId as AncestorId, 0 as distance
from RelationHierarchy
where CHildId = ?
union all
select h.ParentId, a.distance + 1
from ancestor a inner join RelationHierarchy rh on a.AncestorId = rh.ChildId
)
select AncestorId
from ancestor
where distance = (select max(distance) from ancestor)
Something like this will work for above example:
SELECT ParentId FROM RelationHierarchy
WHERE ParentId NOT IN (SELECT CHildId FROM RelationHierarchy)
The only way you can do this in "standard" SQL is to assume a maximum depth for the tree, and then do joins for each level. The following gets the top level id:
select rh1.ChildId,
coalesce(rh4.parentid, rh3.parentid, rh2.parentid, rh1.parentid) as topLevel
from RelationshipHierarchy rh1 left outer join
RelationshipHierarchy rh2
on rh1.parentId = rh2.childId left outer join
RelationshipHierarchy rh3
on rh2.parentId = rh3.childId left outer join
RelationshipHierarchy rh4
on rh3.parentId = rh4.childId;
If you want the name, you can just join it in:
select rh1.ChildId,
coalesce(rh4.parentid, rh3.parentid, rh2.parentid, rh1.parentid) as topLevel,
p.name
from RelationshipHierarchy rh1 left outer join
RelationshipHierarchy rh2
on rh1.parentId = rh2.childId left outer join
RelationshipHierarchy rh3
on rh2.parentId = rh3.childId left outer join
RelationshipHierarchy rh4
on rh3.parentId = rh4.childId left outer join
Person p
on p.id = coalesce(rh4.parentid, rh3.parentid, rh2.parentid, rh1.parentid);
Get all top parents using path
path format: rootId/.../parentId/nodeId/
select t1.path from nodes t1 inner join nodes t2
on t1.path like t2.path+'%'
group by t1.path
having len(t1.path)-len(replace(t1.path, '/', ''))
=min(len(t2.path)-len(replace(t2.path, '/', '')))
Give this a go:
select id,name
from person p
where not exists
(
select 1
from relationhierarchy r
where r.childid= p.id
)
and exists
(
select 1
from relationhierarchy r
where r.parentid= p.id
)
It is not enough to just see if a child id exists as in your example E is present in the person table but not in the relationshiphierarchy table.
WITH CTE_MyTable AS (
SELECT Id, ParentId, NULL As RootParent, 1 As Lvl
FROM dbo.Ministry
UNION ALL
SELECT a.id, b.ParentId, a.ParentId As RootParent, Lvl + 1
FROM CTE_MyTableAS a INNER JOIN
dbo.MyTableAS b ON a.ParentId = b.Id
)
, CTE_Ministry_RN AS (
SELECT Id, RootParent, ROW_NUMBER() OVER (PARTITION BY Id ORDER BY Lvl DESC) RN
FROM CTE_Ministry
)
SELECT Id, ISNULL(RootParent, Id) As RootParent
FROM CTE_Ministry_RN
WHERE RN = 1