SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Require help on CTE common table expressions query construncting


Require help on CTE common table expressions query construncting

Author
Message
nag_arjun123
nag_arjun123
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 43
Table: workgroup id - primary key
id, name
== ====
1 w1
2 w2
3 w3

Table: bizunit : id - primary key ; parent_id - foreignkey to same table column Id
id, name, parent_id
== ====== ========
1 b1 0
2 b2 1
3 b3 1
4 b4 2
5 b5 4
6 b6 3
7 b7 0

Table: Users id- primary key
id, name
1 u1
2 u2

Table: workgroup_bizunit (wgid,bid) --> primary key
wgid, bid
==== ===
2 2
3 2
2 3

Table: users_workgroup (uid,wgid) - primary key
uid, wgid
=== =====
1 1
2 2
2 3

The above given are the table structure and having the sample data.

Now , i have to construct a query based on these tables . could anyone help regarding this.

The input parameter would be the User Id : Lets say I want the details of business units, workgroup which are associated to User Id - 2

In the application The business units tree view structure would be like this
--------------------------------------------------------------------------
Business unit b2 is associated to workgroups w2,w3. And User Id 2 is associated to workgroups w2,w3.
Business unit b3 is associated to workgroup w2.

System
|
|________b2
| |________b4
| |________b5
|
|________b3
|
|________b6


The Output Should be

b2, w2
b2, w3
b3, w2
b4, w2
b4, w3
b5, w2
b5, w3
b6, w2

I want business units b4,b5 in the output because associated to its workgroup thru parent business unit b2.
I want business units b6 in the output because associated to its workgroup thru parent business unit b3.

I have to user CTE common table expressions. Please anyone help regarding this query
Razvan Socol
Razvan Socol
SSC Eights!
SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)SSC Eights! (975 reputation)

Group: General Forum Members
Points: 975 Visits: 129
This looks like a homework assignment, and we don't like solving homeworks for others.

See example D in http://msdn.microsoft.com/en-us/library/ms175972.aspx and try to modify it yourself to fit your purpose.

Razvan Socol
SQL Server MVP



The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7026 Visits: 6903
Applause... thunderous applause.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Matt Miller (4)
Matt Miller (4)
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15749 Visits: 18771
Now - keep in mind this: should you TRY to modify the code listed above, and you get stuck somewhere, and you were to come back and post what you tried (and you make it look like it's NOT homework)... You will likely get quite a bit of help....Smile

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
noeld
noeld
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12316 Visits: 2048
I know I am going to get some heat because of it but hey USA is a free country.


declare @user_id int
select @user_id = 2
;with cte
AS
(
select b.id, b.parent_id, b.name as bunit, w.id as wid, w.name as wgrp
from bizunit b
inner join
workgroup_bizunit wb on b.id = wb.bid
inner join
workgroup w on wb.wgid = w.id
inner join
users_workgroup uw on uw.wgid = wb.wgid
where uw.uid = @user_id
union all
select b.id, b.parent_id, b.name as bunit, w.id as wid, w.name as wgrp
from bizunit b
inner join cte
on b.parent_id = cte.id
inner join
workgroup w on cte.wid = w.id
)
select bunit, wgrp
from cte
order by bunit, wgrp




* Noel
Matt Miller (4)
Matt Miller (4)
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15749 Visits: 18771
That's ok Noel - Chris harshman already posted something similar on the OTHER thread the OP started on the same topic....:

http://www.sqlservercentral.com/Forums/Topic626424-360-1.aspx

enjoy!

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
noeld
noeld
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12316 Visits: 2048
Ha! I didn't follow the link so I have a deserved "Ooops!" Cross-posting hits again!


* Noel
The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 7026 Visits: 6903
At least this one didn't have URGENT URGENT URGENT in the topic header. Tongue

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
noeld
noeld
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12316 Visits: 2048
Bob Hovious (12/29/2008)
At least this one didn't have URGENT URGENT URGENT in the topic header. Tongue


Good Point!


* Noel
Chris Harshman
Chris Harshman
SSCertifiable
SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)SSCertifiable (6.5K reputation)

Group: General Forum Members
Points: 6494 Visits: 4248
Matt Miller (12/29/2008)
That's ok Noel - Chris harshman already posted something similar on the OTHER thread the OP started on the same topic....:

http://www.sqlservercentral.com/Forums/Topic626424-360-1.aspx

enjoy!

Rolleyes Oh well, I just happened to see the other "original" post first, didn't realize this was cross posted in this forum. Also guess I give people too much the benefit of the doubt that this was just a simplification of an actual problem they were trying to solve as opposed to a homework assignment.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search