|
|
|
Forum 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
|
|
|
|
|
Say Hey Kid
      
Group: General Forum Members
Last Login: Thursday, December 13, 2012 1:38 AM
Points: 693,
Visits: 123
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
Applause... thunderous applause.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller Stop, children, what's that sound? -- Stephen Stills
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:56 PM
Points: 6,997,
Visits: 13,941
|
|
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?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:39 PM
Points: 6,260,
Visits: 1,977
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 1:56 PM
Points: 6,997,
Visits: 13,941
|
|
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?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:39 PM
Points: 6,260,
Visits: 1,977
|
|
Ha! I didn't follow the link so I have a deserved "Ooops!" Cross-posting hits again!
* Noel
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Today @ 10:41 AM
Points: 3,788,
Visits: 5,538
|
|
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
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Tuesday, May 14, 2013 4:39 PM
Points: 6,260,
Visits: 1,977
|
|
Bob Hovious (12/29/2008) At least this one didn't have URGENT URGENT URGENT in the topic header. :P
Good Point!
* Noel
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 7:19 AM
Points: 1,562,
Visits: 1,716
|
|
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.
|
|
|
|