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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Select Question?

Author  Topic 

raysefo
Constraint Violating Yak Guru

260 Posts

Posted - 2007-02-03 : 07:40:29
Hi,

i have three tables. Element table,code table and Result table.
Code table has 2 fields, codetid and code.

codeid code
1 a
2 b
3 c
4 d

Result table has elementid,value1 and value2 fields.

elementid value1 value2
1 20 10
2 30 5
3 10 6
4 12 11

element table has elementname and codeid fields

codeid elementname
1 silver
2 silver
3 silver
4 silver
1 copper
2 copper
3 copper
4 copper
.. ...

what i wanna do is to get a result set like below;

elementname, value1 for a, value2 for a, value1 for b, value2 for b,value1 for c, value2 for c,value1 for d, value2 for d.

silver , 20,10,30,5,10,6,12,11

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-02-03 : 19:08:47
[code]
select e.elementname,
[value1 for a] = max(case when c.code = 'a' then r.value1 end),
[value2 for a] = max(case when c.code = 'a' then r.value2 end),
[value1 for b] = max(case when c.code = 'b' then r.value1 end),
[value2 for b] = max(case when c.code = 'b' then r.value2 end),
. . .
from element e inner join result r
on e.codeid = r.elementid
inner join code c
on e.codeid = c.codeid
group by e.elementname
[/code]


KH

Go to Top of Page
   

- Advertisement -