DB2:field in column name

How to have a cross fields table in db2 sql?
I have a table with 3 field: code, week, value.
I want to have starting from this table a second table
with fields: code, week(n), week(n+1).... , week(m)
and in field week I want to put field value of the first table.
A cross fields table.
How to do it?
thanks
bobdylan75Asked:
Who is Participating?
 
bobdylan75Author Commented:
Infact, but with the "case wehn then else end" statment I can do what I need...
in the link below you can say an example,
http://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/
thanks
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi Bob,

DB2 doesn't have a cross-tab function like SQL Server does so you'll have to "roll your own" on this.  That is, if you really need it at all.

You can quite easily just write SQL to put as few or as many columns as you want on the result.

If you can explain what you're trying to do a bit more perhaps we can work out the SQL.


Kent
0
 
abbas_najafizadehCommented:
select *
from
table1 inner join table2
on table1.code=table2.code
and table1.week= (case table1.value when 1 then table2.week1
when 2 then table2.week2
when 3 then table2.week3
when 4 then table2.week4
end)
;

be success

0
 
bobdylan75Author Commented:
thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.