| Author |
Topic |
|
dfribley
Starting Member
2 Posts |
Posted - 2006-02-11 : 17:34:31
|
| "Hi Everybody"With the following tables:Table TBLStages (id,desc)1, 'Stage 1'2, 'Stage 2'3, 'Stage 3'Table TBLStatus (id, FK_stage_id, desc)1, 1, 'status 1-1'2, 1, 'status 1-2'3, 2, 'status 2-1'4, 2, 'status 2-2'5, 3, 'status 3-1'6, 3, 'status 3-2'Table TBLPropStatus (FK_prop_id, FK_status_id, dateTime)1, 1, 1/1/001, 3, 1/1/002, 1, 2/5/013, 1, 3/1/02I'm having a hard time putting together a query that does what I want. Basically, for every FK_prop_id, I want to be able to return a query like this:FOR FK_prop_id = 1:--------------------Stage 1: status 1-1Stage 2: status 2-1Stage 3: NULLI know the starting point would be to query all of TBLStages and LEFT OUTER JOIN the the other tables, but I'm not experienced enough to know how to join the other two tables before joing the Stage table.Sorry if this doesn't make sense; thank you for your help!-Dave Fribley |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2006-02-11 : 18:05:51
|
I have no idea if this query will hold up for different scenarios but for this sample data and for prop_id = 1, it seems to work:use pubsgoset nocount oncreate Table TBLStages (id int primary key, [desc] varchar(20))insert tblStagesselect 1, 'Stage 1' union allselect 2, 'Stage 2' union allselect 3, 'Stage 3'gocreate Table TBLStatus (id int primary key, FK_stage_id int references tblstages(id), [desc] varchar(20))insert tblstatusselect 1, 1, 'status 1-1' union allselect 2, 1, 'status 1-2' union allselect 3, 2, 'status 2-1' union allselect 4, 2, 'status 2-2' union allselect 5, 3, 'status 3-1' union allselect 6, 3, 'status 3-2'gocreate Table TBLPropStatus (FK_prop_id int, FK_status_id int references tblstatus(id), dt dateTime)insert tblPropStatusselect 1, 1, '1/1/00' union allselect 1, 3, '1/1/00' union allselect 2, 1, '2/5/01' union allselect 3, 1, '3/1/02'goselect a.[desc] stageDesc ,b.[desc] statusDescfrom tblStages aleft join ( select a.fk_stage_id, a.[desc] from tblstatus a join tblPropStatus b on b.fk_status_id = a.id where fk_prop_id = 1 ) b on b.fk_stage_id = a.iddrop table tblpropstatusgodrop table tblstatusgodrop table tblstages EDIT:OutputstageDesc statusDesc -------------------- -------------------- Stage 1 status 1-1Stage 2 status 2-1Stage 3 NULL Be One with the OptimizerTG |
 |
|
|
dfribley
Starting Member
2 Posts |
Posted - 2006-02-11 : 18:23:59
|
Thanks TG That answered my question. I was having a huge brain fart, and it was nice to take a quick glance at your code.-Dave Fribley |
 |
|
|
|
|
|