Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Require help on CTE common table expressions query construncting Expand / Collapse
Author
Message
Posted Monday, December 29, 2008 12:15 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, September 24, 2009 2:10 AM
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
Post #626425
Posted Monday, December 29, 2008 3:44 AM
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Friday, September 6, 2013 2:16 AM
Points: 693, Visits: 124
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



Post #626459
Posted Monday, December 29, 2008 10:24 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
Applause... thunderous applause.

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #626661
Posted Monday, December 29, 2008 10:31 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:32 PM
Points: 7,152, Visits: 15,633
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....:)

----------------------------------------------------------------------------------
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?
Post #626671
Posted Monday, December 29, 2008 10:41 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:34 AM
Points: 6,259, Visits: 2,031
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
Post #626683
Posted Monday, December 29, 2008 10:43 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 4:32 PM
Points: 7,152, Visits: 15,633
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?
Post #626686
Posted Monday, December 29, 2008 10:46 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:34 AM
Points: 6,259, Visits: 2,031
Ha! I didn't follow the link so I have a deserved "Ooops!" Cross-posting hits again!



* Noel
Post #626689
Posted Monday, December 29, 2008 11:12 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Sunday, August 17, 2014 3:10 PM
Points: 2,787, Visits: 6,098
At least this one didn't have URGENT URGENT URGENT in the topic header. :P

__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? -- Stephen Stills
Post #626699
Posted Monday, December 29, 2008 11:20 AM
SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 6:34 AM
Points: 6,259, Visits: 2,031
Bob Hovious (12/29/2008)
At least this one didn't have URGENT URGENT URGENT in the topic header. :P


Good Point!



* Noel
Post #626706
Posted Monday, December 29, 2008 1:47 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, November 18, 2014 8:13 AM
Points: 1,931, Visits: 2,100
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!

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.
Post #626830
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse