Find all parents of given ids in tree in SQL Server - sql-server

I have a tree structure in single table and item relationships in another table. I need to find out all the parents from the given items.
SELECT Id FROM dbo.Items WHERE Selected = 1
Since I get 6,9, 11 from that query, I would need to return parent item ids of those which are 7, 5, 2.
I think I should use some kind of recursive CTE for this, but I'm not sure where to start.
Could you help me out? Thanks!
dbo.Relationship
Id ParentId
3 6
6 7
8 7
7 2
4 9
9 5
5 2
dbo.Items
Id Selected
2 0
3 0
4 0
5 0
6 1
7 0
8 0
9 1
11 1

Do you want to get all the parent of selected items?
I changed to use temporary table.
if object_id('tempdb..#Relationship') is not null drop table #Relationship
create table #Relationship(Id int,ParentId int)
insert into #Relationship(Id,ParentId)
SELECT 3,6 UNION
SELECT 6,7 UNION
SELECT 8,7 UNION
SELECT 7,2 UNION
SELECT 4,9 UNION
SELECT 9,5 UNION
SELECT 5,2
if object_id('tempdb..#items') is not null drop table #items
create table #items(Id int, Selected bit)
insert into #items(Id,Selected)
SELECT 2,0 UNION
SELECT 3,0 UNION
SELECT 4,0 UNION
SELECT 5,0 UNION
SELECT 6,1 UNION
SELECT 7,0 UNION
SELECT 8,0 UNION
SELECT 9,1 UNION
SELECT 11,1
;with cte AS (
SELECT i.ID AS SelectedID,r.ParentId FROM #Items AS i INNER JOIN #Relationship AS r ON i.id=r.id WHERE i.Selected=1
UNION ALL
SELECT cte.SelectedID, r.ParentId FROM #Relationship AS r INNER JOIN CTE ON CTE.ParentId=r.id
)
SELECT * FROM cte ORDER BY cte.SelectedID
Can it give you some help?
SelectedID ParentId
----------- -----------
6 7
6 2
9 5
9 2

Left Join on the related Id between Items and Relations.
SELECT
Items.Id,
Relationship.ParentId
FROM Items
LEFT JOIN Relationship ON Relationship.Id = Items.Id
I have used this in the past to get all parent Ids:
with compParent as
(
select * from Component Where ComponentId = #ComponentId
union all
select Component.* from Component join compParent on Component.ComponentId =
compParent.ContainerParentId
)
select * from compParent;
I have used this to get all children:
with compChild as
(
select * from Component where ComponentId = #ParentId
union all
select Component.* from Component join compChild on Component.ContainerParentId = compChild.ComponentId
)
select * from compChild;
You can also look at the many stackOverFlow posts that are already out there to get parent and/or children. Or a simple google search for "SQL Server Get Parents"

Related

Filter table to show only most recent values [duplicate]

This question already has answers here:
Get top 1 row of each group
(19 answers)
Closed 11 months ago.
I have a table that looks like this.
Category
Type
fromDate
Value
1
1
1/1/2022
5
1
2
1/1/2022
10
2
1
1/1/2022
7.5
2
2
1/1/2022
15
3
1
1/1/2022
3.5
3
2
1/1/2022
5
3
1
4/1/2022
5
3
2
4/1/2022
10
I'm trying to filter this table down to filter down and keep the most recent grouping of Category/Type. IE rows 5 and 6 would be removed in the query since they are older records.
So far I have the below query but I am getting an aggregate error due to not aggregating the "Value" column. My question is how do I get around this without aggregating? I want to keep the actual value that is in the column.
SELECT T1.Category, T1.Type, T2.maxDate, T1.Value
FROM (SELECT Category, Type, MAX(fromDate) AS maxDate
FROM Table GROUP BY Category,Type) T2
INNER JOIN Table T1 ON T1.Category=T2.Category
GROUP BY T1.Category, T1.Type, T2.MaxDate
This has been asked and answered dozens and dozens of times. But it was quick and painless to type up an answer. This should work for you.
declare #MyTable table
(
Category int
, Type int
, fromDate date
, Value decimal(5,2)
)
insert #MyTable
select 1, 1, '1/1/2022', 5 union all
select 1, 2, '1/1/2022', 10 union all
select 2, 1, '1/1/2022', 7.5 union all
select 2, 2, '1/1/2022', 15 union all
select 3, 1, '1/1/2022', 3.5 union all
select 3, 2, '1/1/2022', 5 union all
select 3, 1, '4/1/2022', 5 union all
select 3, 2, '4/1/2022', 10
select Category
, Type
, fromDate
, Value
from
(
select *
, RowNum = ROW_NUMBER() over(partition by Category, Type order by fromDate desc)
from #MyTable
) x
where x.RowNum = 1
order by x.Category
, x.Type

SQL Server, subset a table and create new column based on condition

If I have a table in SQL as:
id
code
1
6
1
8
1
4
2
3
2
7
2
4
3
7
3
6
3
7
What I need to do, logically is:
Get the top row of each group when grouped by id, ordered by code
Create a new column to show if the code column contained a 7 anywhere, within the group
Desired result:
id
code
c7
1
4
N
2
3
Y
3
6
Y
I think it needs a "CASE WHEN" statement in the SELECT, but have not worked it out. What query can I execute to get this?
Seems like you can use a MIN and a conditional aggregate:
SELECT id,
MIN(Code) AS Code,
CASE WHEN COUNT(CASE code WHEN 7 THEN 1 END) > 0 THEN 'Y' ELSE 'N' END AS C7
FROM dbo.YourTable
GROUP BY id;
There is probably a better way to do this, but what comes to mind is that first you have to partition the table to get the top one based on whatever that criteria is, then join back against itself to find the ones with the 7
declare #table1 table (id int not null, code int not null)
insert into #table1 (id, code)
values
(1,6),
(1,8),
(1,4),
(2,3),
(2,7),
(2,4),
(3,7),
(3,6),
(3,7)
select id, code, c7
from (
select t.id ,t.code
,(CASE WHEN c.id is null then 'N' else 'Y' END) as c7
,ROW_NUMBER() OVER (PARTITION BY t.id order by t.code) AS p
from #table1 t
left outer join (
select id, code, 'Y' as c7
from #table1
where code = 7) c on c.id = t.id
) sorted
where sorted.p = 1

Create a view using SQL Server with repeating rows and new column

I have a table with the following columns.
EVAL_ID | GGRP_ID | GOAL_ID
1 1 1
2 2 1
2 2 2
3 1 3
I want to create a view with another columns called GOAL_VERSION which has values from 1 to 3. So that each row from the above table should be duplicated 5 times for different GOAL_VERSION numbers. The out put should be like this.
EVAL_ID | GGRP_ID | GOAL_ID |GOAL_VERSION
1 1 1 1
1 1 1 2
1 1 1 3
1 1 1 4
1 1 1 5
2 2 1 1
2 2 1 2
2 2 1 3
2 2 1 4
2 2 1 5
How can I do that. Help me. Thank you.
Is it this you are looking for?
DECLARE #tbl TABLE(EVAL_ID INT,GGRP_ID INT,GOAL_ID INT);
INSERT INTO #tbl VALUES
(1,1,1)
,(2,2,1)
,(2,2,2)
,(3,1,3);
SELECT tbl.*
,x.Nr
FROM #tbl AS tbl
CROSS JOIN (VALUES(1),(2),(3),(4),(5)) AS x(Nr)
EDIT: Varying count of repetition
DECLARE #tbl TABLE(EVAL_ID INT,GGRP_ID INT,GOAL_ID INT);
INSERT INTO #tbl VALUES
(1,1,1)
,(2,2,1)
,(2,2,2)
,(3,1,3);
DECLARE #tblCountOfRep TABLE(CountOfRep INT);
INSERT INTO #tblCountOfRep VALUES(3);
SELECT tbl.*
,y.Nr
FROM #tbl AS tbl
CROSS JOIN (SELECT TOP (SELECT CountOfRep FROM #tblCountOfRep) * FROM(VALUES(1),(2),(3),(4),(5) /*add the max count here*/) AS x(Nr)) AS y
In this case I'd prefer I numbers table...
Take a look at CROSS JOIN. If you make a table that's got one column with the 5 rows you want you can just CROSS JOIN it to get the result you're after.
You can achieve this using a CTE and CROSS APPLY:
;WITH CTE AS
(
SELECT 1 AS GOAL_VERSION
UNION
SELECT 2
UNION
SELECT 3
UNION
SELECT 4
UNION
SELECT 5
)
SELECT * FROM <your table>
CROSS APPLY CTE
use "with" (cte) with rank clause for creating view.
If you have a numbers table in SQL database, you can cross join your table with the numbers table for numbers between 1 and 5
Here is my SQL solution for your requirement
select
goals.*,
n.i as GOAL_VERSION
from goals, dbo.NumbersTable(1,5,1) n
And here is the modified version with "cross join" as suggested in the comments
select
goals.*,
n.i as GOAL_VERSION
from goals
cross join dbo.NumbersTable(1,5,1) n
You can realize, I used a SQL table-valued function for SQL numbers table
Please create that SQL function using the source codes given in the referred tutorial
I hope it helps,

Using Recursive CTE with GroupBy

I am new to the recursive CTE concept and a problem at hand, I got a tiny feeling that the problem can be solved by using recursive CTE. Let me know what you guys think.
Two tables:
Table one is a self referencing Location table with ID, ParentID, Level and Description.
Table two is an asset table which records individual assets and has a foreign key to Location table ID field.
Table1:
ID Description ParentID Level
1 Site1 NULL 1
2 Site2 NULL 1
3 Building1 1 2
4 Building2 1 2
5 Floor1 3 3
6 Floor2 3 3
7 Floor3 4 3
8 Place1 5 4
9 Place2 7 4
Table2:
ID Description Quantity LocationID
1 Desk 3 8
2 Lamp 1 8
3 PC 10 9
I would like to create a stored procedure with a input parameter of #Level and returns all the Location records at that level and the number of assets within the location (including sub levels).
For example, if #Level = 3, the stored procedure should return:
ID Description AssetCount
5 Floor1 4
6 Floor2 0
7 Floor3 10
If #Level = 2, the stored procedure should return:
ID Description AssetCount
3 Building1 4
4 Building2 10
If the problem is not clear, please let me know.
Well, nothing special here, just a recursive CTE joined with the other table, and the results are what you expected:
declare #level int = 3
;with CTE as (
select id as origid, id, Description, parentid
from table1 where level = #level
union all
select CTE.origid, t1.id, CTE.Description, t1.parentid
from CTE join table1 t1 on
CTE.id = t1.parentid
)
select origid, CTE.description, isnull(sum(t2.Quantity),0) as Quantity
from CTE left outer join table2 t2 on CTE.id = t2.locationid
group by origid, CTE.description
SQL Fiddle

Search within ColA duplicates against specific unique vals in ColB to exclude all of ColA

I apologize in advance I feel like I'm missing something really stupid simple. (and let's ignore database structure as I'm kind of locked into that).
I have, let's use customer orders - an order number can be shipped to more than one place. For the sake of ease I'm just illustrating three but it could be more than that (home, office, gift, gift2, gift 3, etc)
So my table is:
Customer orders:
OrderID MailingID
--------------------
1 1
1 2
1 3
2 1
3 1
3 3
4 1
4 2
4 3
What I need to find is OrderIDs that have been shipped to MailingID 1 but not 2 (basically what I need to find is orderID 2 and 3 above).
If it matters, I'm using Sql Express 2012.
Thanks
Maybe this could help:
create table #temp(
orderID int,
mailingID int
)
insert into #temp
select 1, 1 union all
select 1, 2 union all
select 1, 3 union all
select 2, 1 union all
select 3, 1 union all
select 3, 3 union all
select 4, 1 union all
select 4, 2 union all
select 4, 3
-- find orderIDs that have been shipeed to mailingID = 1
select
distinct orderID
from #temp
where mailingID = 1
except
-- find orderIDs that have been shipeed to mailingID = 2
select
orderID
from #temp
where mailingID = 2
drop table #temp
A simple Subquery With NOT IN Operator should work.
SELECT DISTINCT OrderID
FROM <tablename> a
WHERE orderid NOT IN (SELECT orderid
FROM <tablename> b
WHERE b.mailingID = 2)

Resources