how do i get hierarchy of data from two different tables? - sql-server

I have two tables TwitterStatus and twitterstatusDetails,
I have data respectively in following manner.
Here as given in twitterstatus table 4th record with the ts_StatusID "587573444633427968" is parent of twitterstatus_Details table 3rd record, which having td_inReplyTostatusID value is "587573444633427968", for next record td_StatusID of recent child record is parent of twitterstatus table having that child ts_StatusID value in ts_InreplytoStatusID field, so like that it is the hierarchy, so
how can I get the records from both the tables based on child parent relationship?
still any concern then please ask in comment.
Thank you.

I think what you are looking for is recursive CTE to get every nested level.
Hope this get you closer to your goal.
--REPLACE #TwitterStatus and #twitterstatus_Details with your tablename.
;WITH CteTStatus
AS
(
SELECT h.*, d.*, 0 as lvl
FROM #TwitterStatus AS h
INNER JOIN #twitterstatus_Details AS d ON h.id_StatusID = d.td_InreplytoStatusID
WHERE ts_StatusID = '587573444633427968'--WHERE CLAUSE TO GET ROOT PARENT
UNION ALL -- UNION TO DIG INTO CHILD's
SELECT h.*, d.*, lvl + 1 as lvl
FROM #TwitterStatus AS h
INNER JOIN #twitterstatus_Details AS d ON h.id_StatusID = d.td_InreplytoStatusID
INNER JOIN CteTStatus AS Parent ON h.ts_InreplytoStatusID = Parent.td_StatusID
)
SELECT *
FROM CteTStatus

Related

How use recursive CTE content for specific child node?

I have a recursive CTE query in SQL Server which handles getting the depth of each parent and child values. However I want to get the full depth for only on specific child. For example the data I have looks something like this, I only want to get the results for where c_key is equal to child my initial query below the table gets the results for all links.
I tried to add a where clause in to the unioned select statement, but this just returned the the 0th depth rows.
c_key
p_key
child
teen
teen
adult
adult
old
young
middle
middle
old
WITH cte_data (c_key, p_key, depth) AS
(
SELECT c_key, p_key, 0 AS depth
FROM my_table
WHERE p_key = 'old'
UNION ALL
SELECT c.c_key, c.p_key, o.depth + 1 AS depth
FROM cte_data o
INNER JOIN my_table c ON o.c_key = c.p_key
)
SELECT *
FROM cte_data
Seemingly, you should swap columns for the join condition and change the where search condition according to your task condition.
WITH cte_data (c_key, p_key, depth) AS
(
SELECT c_key, p_key, 0 AS depth
FROM my_table
WHERE c_key = 'child'
UNION ALL
SELECT c.c_key, c.p_key, o.depth + 1 AS depth
FROM cte_data o
INNER JOIN my_table c ON o.p_key = c.c_key
)
SELECT *
FROM cte_data
ORDER by depth

How to update multiple rows in a temp table with multiple values from another table using only one ID common between them?

I am trying to reconcile the IDs in a temp table (top) from another DB table (bottom). Since I only have one ID that's common between the two, I am only getting the top result for all the rows (ReconGlobalRemunerationGrantID) in my temp table. I am aiming to get each of the unique ID and update my temp table as such.
Right now, my update query is simple and I update using the ID common between the 2 tables. Is there a function or another command statement I can use to get the result intended?
update tgrg set ReconGlobalRemunerationGrantID = grg.GlobalRemunerationGrantID from #GlobalRemunerationGrant tgrg
join #GlobalRemuneration tgr on tgr.GlobalRemunerationID = tgrg.GlobalRemunerationID
join DataCore..GlobalRemuneration gr on gr.CompanyID = #CompanyID and gr.FiscalYearID = tgr.FiscalYearID and gr.DirectorDetailID = tgr.DirectorDetailID and tgr.GlobalRoleIDCODE = gr.GlobalRoleID
join DataCore..GlobalRemunerationGrant grg on gr.GlobalRemunerationID = grg.GlobalRemunerationID
Thank you.
Based on the comment - you have 2 values to match on, not just one? e.g., both GlobalRemunerationID and GlobalRemunerationGrantID?
Here's an example using tables 'temptable' and 't1'
UPDATE temptable
SET ReconGlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
FROM temptable
INNER JOIN t1 ON temptable.GlobalRemunerationID = t1.GlobalRemunerationID
AND temptable.GlobalRemunerationGrantID = t1.GlobalRemunerationGrantID
Update below
The below version takes the two data sets
Partitions them by GlobalRemunerationID and orders them by ReconGlobalRemunerationGrantID to get the 'row numbers' (rn)
Joins them on GlobalRemunerationID and rn to get them in order
Key code is below (with slightly different tables than your full set sorry - matches the data set you gave though).
; WITH tgrg AS
(SELECT GlobalRemunerationID, ReconGlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM #GlobalRemunerationGrant
)
UPDATE tgrg
SET ReconGlobalRemunerationGrantID = tgr.GlobalRemunerationGrantID
FROM tgrg
INNER JOIN
(SELECT GlobalRemunerationID, GlobalRemunerationGrantID,
ROW_NUMBER() OVER (PARTITION BY GlobalRemunerationID ORDER BY GlobalRemunerationGrantID) AS rn
FROM GlobalRemuneration
) AS tgr ON tgrg.GlobalRemunerationID = tgr.GlobalRemunerationID AND tgrg.rn = tgr.rn
A db<>fiddle with the full set is there - note that I changed some of the IDs to demonstrate that it wasn;t using them to match.

I have Child,Mother M:M Table.How to select Child with all common Mother their has?

I have 3 table with Mother, Child, and Junction like this
Mother
Child (ChildName ZZZ Should Have 53 Code)
Junction
I want to use Mother (at least 2) as Where Condition to know what child their has in common
like
Select CodeChild
From Junction
Where CodeMother in ('02','03')
What I expect to result
Can't test this
Select Junction.CodeChild, Child.ChildName From Junction
Inner Join Child On Junction.ChildCode = Child.CodeChild
Where Junction.CodeMother = CodeMother
As per OP Edit
Solved but appreciate any better ways
Since Mother condition can have 1-4 the query depends on that
For Example
Select r.ChildName
From (Select j.CodeChild From Junct j Where j.CodeMother = 2)M1
inner join (Select j.CodeChild From Junct j Where j.CodeMother = 3)M2 On
M2.CodeChild = M1.CodeChild
//If More Mother Condition Just put inner join M3,M4 and So on Here
inner join Child r on M1.CodeChild = r.CodeChild

RIGHT\LEFT Join does not provide null values without condition

I have two tables one is the lookup table and the other is the data table. The lookup table has columns named cycleid, cycle. The data table has SID, cycleid, cycle. Below is the structure of the tables.
If you check the data table, the SID may have all the cycles and may not have all the cycles. I want to output the SID completed as well as missed cycles.
I right joined the lookup table and retrieved the missing as well as completed cycles. Below is the query I used.
SELECT TOP 1000 [SID]
,s4.[CYCLE]
,s4.[CYCLEID]
FROM [dbo].[data] s3 RIGHT JOIN
[dbo].[lookup_data] s4 ON s3.CYCLEID = s4.CYCLEID
The query is not displaying me the missed values when I query for all the SID's. When I specifically query for a SID with the below query i am getting the correct result including the missed ones.
SELECT TOP 1000 [SID]
,s4.[CYCLE]
,s4.[CYCLEID]
FROM [dbo].[data] s3 RIGHT JOIN [dbo].[lookup_data] s4
ON s3.CYCLEID = s4.CYCLEID
AND s3.SID = 101002
ORDER BY [SID], s4.[CYCLEID]
As I am supplying this query into tableau I cannot provide the sid value in the query. I want to return all the sid's and from tableau I will be do the rest of the things.
The expected output that i need is as shown below.
I wrote a cross join query like below to acheive my expected output
SELECT DISTINCT
tab.CYCLEID
,tab.SID
,d.CYCLE
FROM ( SELECT d.SID
,d.[CYCLE]
,e.CYCLEID
FROM ( SELECT e.sid
,e.CYCLE
FROM [db_temp].[dbo].[Sheet3$] e
) d
CROSS JOIN [db_temp].[dbo].[Sheet4$] e
) tab
LEFT OUTER JOIN [db_temp].[dbo].[Sheet3$] d
ON d.CYCLEID = tab.CYCLEID
AND d.SID = tab.SID
ORDER BY tab.SID
,tab.CYCLEID;
However I am not able to use this query for more scenarios as my data set have nearly 20 to 40 columns and i am having issues when i use the above one.
Is there any way to do this in a simpler manner with only left or right join itself? I want the query to return all the missing values and the completed values for the all the SID's instead of supplying a single sid in the query.
You can create a master table first (combine all SID and CYCLE ID), then right join with the data table
;with ctxMaster as (
select distinct d.SID, l.CYCLE, l.CYCLEID
from lookup_data l
cross join data d
)
select d.SID, m.CYCLE, m.CYCLEID
from ctxMaster m
left join data d on m.SID = d.SID and m.CYCLEID = d.CYCLEID
order by m.SID, m.CYCLEID
Fiddle
Or if you don't want to use common table expression, subquery version:
select d.SID, m.CYCLE, m.CYCLEID
from (select distinct d.SID, l.CYCLE, l.CYCLEID
from lookup_data l
cross join data d) m
left join data d on m.SID = d.SID and m.CYCLEID = d.CYCLEID
order by m.SID, m.CYCLEID

Sql Recursive Function only show nods that have specifik relation

I have a Link structure table with ID and parentID.
ID, Parent, name
1,1
2,1
3,2
4,3
5,3
To this table I have a structure_article relation table
in this table I have relation between a Link and a article.
struture_article
structid, articleID
4,1000
4,1001
5,1002
Every article in that table have a supplier.
Now i am trying to create a recursive function that creates the tree
nods if i pick a specific supplier.
Article table
ArticleID, SUPPLIER ID
1000,1
1001,2
1002,2
If I pick articles with supplier 1 then I want the function to show me the tree structure that have articles from that supplier.
I have 20 suppliers and 300 links in the DB now i want only to show articles from the suppliers i pick. I don want any empty nods.
Is this even possible do create with a recursive function in Sql Server version 2008?
I tyied wiht this code the problem is that i get only nods that have articles connected
WITH a
AS (SELECT *
FROM structure
WHERE parent = 125
UNION ALL
SELECT m.*
FROM structure m
JOIN a
ON m.parent = a.internidstructure)
SELECT *
FROM a
WHERE internidstructure IN (SELECT DISTINCT( internidstructure )
FROM dbo.articles
INNER JOIN dbo.structure_article
ON dbo.articles.internidarticle =
dbo.structure_article.internidarticle
WHERE ( dbo.articles.internidsupplier IN (SELECT
internidsupplier
FROM site_sup
WHERE
internidsite = 1) ))
ORDER BY parent,
sortno
Try using a left join instead of an inner join.
I'm not sure to have understood your need but if you want to have a new tree table without nodes not linked to a supplier, this query can work.
WITH A AS
(
SELECT S.ID as ID, S.Parent as Parent, 1 as art_linked
FROM structure S
INNER JOIN dbo.structure_article SA
ON S.ID = SA.structid
INNER JOIN Article AR
ON AR.ArticleID = SA.ArticleID
WHERE AR.SupplierID = 1
UNION ALL
SELECT S.ID, S.Parent, 0
FROM structure S
INNER JOIN A
ON A.parent = S.ID
WHERE S.ID <> S.Parent
)
SELECT A.ID, A.Parent, MAX(A.art_linked)
FROM A
GROUP BY A.ID, A.Parent

Resources