Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
ladiaocb
Yak Posting Veteran
57 Posts |
Posted - 2007-02-21 : 12:45:12
|
I'm having a problem with this query. I have the following tables:T1--uid----skill1----skill2----skill31------20--------54--------322------54--------32--------203------54--------20--------32T2--skillid----skillname20---------Level132---------Level254---------Level3I am trying to return the following result:uid----primSkill----Secskill----Altskill1------Level1-------Level3------Level22------Level3-------Level2------Level13------Level3-------Level1------Level2The query I wrote is returning multiple values with NULLS. Any help is greatly appreciated! |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-02-21 : 13:25:05
|
If you want us to help fix your query, it would be helpful to us if you posted the code for the query.CODO ERGO SUM |
 |
|
ladiaocb
Yak Posting Veteran
57 Posts |
Posted - 2007-02-21 : 13:33:10
|
Sure thing. select max(a.uid) uid, (select b.skillname where a.skill1 = b.skillid) Primskill,(select b.skillname where a.skill2 = b.skillid) Secskill,(select b.skillname where a.skill3 = b.skillid) Altskillfrom t1 a, t2 b where b.skill_id in (a.skill1, a.skill2, a.skill3)group by a.uid, b.skillname, a.skill1, b.skillid, a.skill2, b.skillid, a.skill3, b.skillidIt is grouping the results but it doesn't include them all in the same row. Thanks for any help! |
 |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2007-02-21 : 17:47:08
|
[code]select a.uid, b.skillname as Primskill, c.skillname as Secskill, d.skillname as Altskillfrom t1 a inner join t2 b on a.skill1 = b.skillidinner join t2 c on a.skill2 = c.skillidinner join t2 d on a.skill3 = d.skillid[/code] KH |
 |
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2007-02-21 : 18:41:31
|
if column skill1 ,skill2, or skill3 is nullable, you should use a left join.select a.uid, b.skillname as Primskill, c.skillname as Secskill, d.skillname as Altskillfrom t1 a left join t2 b on a.skill1 = b.skillid left join t2 c on a.skill2 = c.skillid left join t2 d on a.skill3 = d.skillid CODO ERGO SUM |
 |
|
|
|
|
|
|