Solved

Using Common Table Expression for Recursive Queries

Posted on 2010-11-25
8
474 Views
Last Modified: 2012-05-10
Hi,

I have two tables like this:

-----------------------------------------
Table1
-----------------------------------------
id, table2id, value, date, ....

-----------------------------------------
Table2
-----------------------------------------
id, child, ...

And i use table 2 to populate a treeview (0 is the root level):
id child description
1    0    Level 1
2    1    Level 2a
3    1    Level 2b
4    3    Level 3
5    4    Level 4
...

in the end I get this in the treeview:

Level1
----  Level 2a
----  Level 2b
--------  Level 3
------------  Level 4


Now I want to make a recursive query to get all entries in table1 where the Table1.table2id = Table2.ID, to sum all the Table1.values, plus all the descent nodes available.

Example: I choose the Level 2b I want all results for that and also for Level 3 and Level 4 (or more if exists)


I'm working with Common Table Expression (CTE) for recursive queries, using the WITH keyword, but no goal.

Anyone help me on this?


Thanks!
0
Comment
Question by:jpaulino
  • 4
  • 2
  • 2
8 Comments
 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 50 total points
ID: 34214335
0
 
LVL 48

Author Comment

by:jpaulino
ID: 34214400
I have found a lot of links like that, but I'm having problems implementing it :(

I'm trying
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34214454
Not clear what you are after, but this
(1) shows the tree levels
(2) shows the summation of values from table1 for current level + all descendants

/*
create table table1(id int identity, table2id int, value int, date datetime)
insert table1 select 1,2,null
insert table1 select 2,3,null
insert table1 select 3,4,null
insert table1 select 4,5,null
insert table1 select 5,6,null
insert table1 select 5,7,null
insert table1 select 5,8,null
create table table2(id int, child int, description varchar(10))
insert table2 select 1,0,'Level 1'
insert table2 select 2,1,'Level 2a'
insert table2 select 3,1,'Level 2b'
insert table2 select 4,3,'Level 3'
insert table2 select 5,4,'Level 4'
*/
;with cte as (
      select level=1,id,child from table2
     where id=3   --- this is where it all starts, id3=level2b per your question
      union all
      select level+1,a.id,a.child from table2 a
      inner join cte on cte.id=a.child -- parent?! weird naming
), cte2 as (
      select root=id,id,level,sublevel=level+1 from cte
      union all
      select root,a.id,a.level,sublevel+1 from cte a
      inner join cte2 on cte2.id=a.child
)
select replicate('-',min(level)*4-1) + b.description, sum(c.value)
from cte2 a
inner join table2 b on a.root=b.id
inner join table1 c on a.id=c.table2id
group by b.description, root
0
 
LVL 51

Expert Comment

by:HainKurt
ID: 34214456
the one i posted has full script, including table structure, demo data etc...

just you should replace tablename and column names I guess ;)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 48

Author Comment

by:jpaulino
ID: 34214718
Thanks all for the input!

@cyberkiwi,

I have changed the selection a little for my needs (I need grouped by year, month and sum all the tpl's), but i'm not getting the right results ... i'm making some tests. But it's almost right! ;)

select YEAR(c.data), MONTH(c.data),sum(c.tpl)
from cte2 a
inner join dbo.equipamentos b on a.root=b.id
inner join dbo.mcHistorico c on a.id=c.equipamentoID
GROUP BY  YEAR(c.data), MONTH(c.data)
HAVING YEAR(c.data)=2010

I just need the year+month+sum(table1.tpl)


PS: parent was a wrong translactions for the example ... it has a portuguese name :)
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 450 total points
ID: 34214869
> but i'm not getting the right results

Hold on, you want one sum for a particular record+children?  I thought you wanted something more complicated, on each level, like this:

---Level 2b      30    (4 at level 2b + 26 from all children)
-------Level 3      26    (5 at level 3, plus 21 from level 4)
-----------Level 4      21

It looks like all you want is the result at level 2b, summed up, then broken down again by year, month.  So:


/*

create table mcHistorico(id int identity, equipamentoID int, tpl int, data datetime)

insert mcHistorico select 1,2,'20100101'

insert mcHistorico select 2,3,'20100101'

insert mcHistorico select 3,4,'20100101'

insert mcHistorico select 4,5,'20100101'

insert mcHistorico select 5,6,'20100101'

insert mcHistorico select 5,7,'20100101'

insert mcHistorico select 5,8,'19990101' -- not in 2010

create table equipamentos(id int, child int, description varchar(10))

insert equipamentos select 1,0,'Level 1'

insert equipamentos select 2,1,'Level 2a'

insert equipamentos select 3,1,'Level 2b'

insert equipamentos select 4,3,'Level 3'

insert equipamentos select 5,4,'Level 4'

*/

;with cte as (

     select id,child from equipamentos

     where id=3   --- this is where it all starts, id3=level2b per your question

     union all

     select a.id,a.child from equipamentos a

     inner join cte on cte.id=a.child -- link to previous recursion

)

select YEAR(c.data), MONTH(c.data), SUM(c.tpl)

from cte a

inner join mcHistorico c on a.id=c.equipamentoID

where c.data >= '20100101' and c.data < '20110101'

group by YEAR(c.data), MONTH(c.data)

Open in new window

0
 
LVL 48

Author Comment

by:jpaulino
ID: 34214893
>> Hold on, you want one sum for a particular record+children?

Yes, I just want the sum of all records (parent + childs) for a specific time.

I will test that!

Many thanks ;)
0
 
LVL 48

Author Comment

by:jpaulino
ID: 34218834
Works like a charm :)

Thanks for the great help!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
SQL Statement to Update Email Domain 2 23
Can someone plz fix this..getting an error 3 18
Query / Window function ? 3 18
MSSQL: Substring and Charindex error 7 19
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now