Welcome to dbFreaks.com!
FAQFAQ      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Changing layout from rows to columns

 
   Database Help (Home) -> Datamining RSS
Next:  sql query help  
Author Message
Mauricio Harger

External


Since: May 12, 2004
Posts: 1



(Msg. 1) Posted: Wed May 12, 2004 10:16 pm
Post subject: Changing layout from rows to columns
Archived from groups: microsoft>public>sqlserver>datamining (more info?)

Hi,

I am having problems to convert this layout in the way I want.

I have this table:.

Date Code Value
2002 01 101
2002 02 201
2002 03 301
2003 01 102
2003 04 202
2003 05 302
2004 02 103
2004 03 203

And this is the way I would like to receive the report:

Code Value 2002 Value 2003
Value 2004
01 101 102
0
02 201 0
103
03 301 0
203
04 0 202
0
05 0 302
0

The first problem is that some codes does not exist every year, so I have to
put 0 value on it. Second, the number of years can change. Thanks,
--

Mauricio Harger - Audit Brasil Consultores
mauricio RemoveThis @auditbrasil.com.br
+55 47 433-0222

 >> Stay informed about: Changing layout from rows to columns 
Back to top
Login to vote
Vishal Parkar

External


Since: Mar 01, 2004
Posts: 82



(Msg. 2) Posted: Fri May 21, 2004 1:07 am
Post subject: Re: Changing layout from rows to columns [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

hi Mauricio,

on the basis of below sample data see 2 solutions given below.

--sample table and data

create table t
(Date varchar (4),
Code varchar (2),
Value int )
go
insert into t values(2005,'01','101')
insert into t values(2005,'02','201')
insert into t values(2005,'03','301')
insert into t values(2006,'01','102')
insert into t values(2006,'04','202')
insert into t values(2006,'05','302')
insert into t values(2007,'02','103')
insert into t values(2007,'03','203')
go

--soln. 1)

if number of years are fixed and known to you. then you can have query as
follow.
(this hard codes the value.)

select distinct code ,
isnull((select value from t a where a.code = t.code and date = '2002'),0)
'2002',
isnull((select value from t a where a.code = t.code and date = '2003'),0)
'2003',
isnull((select value from t a where a.code = t.code and date = '2004'),0)
'2004'
from t

--soln. 2)

/*
if number of years are dynamic then you will have to make use of dynamic sql
and some procedural code
ex: using dynamic sql
*/

declare @dt int
declare @str varchar(8000)
select @dt = 0,@str=''

while 1=1
begin
select @dt=min(date) from t
where date > @dt

if (select min(date) from t where date > @dt) is null
break

set @str = @str + 'isnull((select value from t a where a.code = t.code and
date = ''' + cast(@dt as varchar(4)) + '''),0) ''' + cast(@dt as varchar(4))
+ ''','
end

set @str = @str + 'isnull((select value from t a where a.code = t.code and
date = ''' + cast(@dt as varchar(4)) + '''),0) ''' + cast(@dt as varchar(4))
+ ''''
set @str = 'select distinct code ,' + @str + ' from t'
exec (@str)

--
Vishal Parkar
vgparkar RemoveThis @yahoo.co.in | vgparkar RemoveThis @hotmail.com

 >> Stay informed about: Changing layout from rows to columns 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Running a SQL query - table include FROM and TO columns - I am trying to export some calendar data from a sql table. This table has 2 column names "from" and "to" for the time range of the appointment. When I run the sql query: select date, from, to, act_code, mat_code, clt_code, notes, de...

Collation Problem any help - Hello all : My problem is like the following I have an online databse with a latin collation, this database is using ntext and nvarchar values that was with our previous admin ok the problem is in the data is stored should use another collation (the...

Reprocessing a mining model - I'm currently developing a user registration prototype. The aim is to classify certain types of users. However, after the registration I query a decision tree model in Analysis Services with DMX and I want to send the user data into the warehouse to....

Trying to cut down query times - I have a query that is based on two other queries that are based on tables in a database. All of the queries that are used for this final query use grouping and totals. In the application I am making, I need to choose a date range to select the subset....

Update serial number in database - Hi, I want to update specific fields in a existing table in SQL database as below example: L240100001-LG L240100002-LG *serial number is difficult to update one by one L240100003-LG L240100004-LG L240100005-LG change to L240100001-L L240100002-L..
   Database Help (Home) -> Datamining All times are: Pacific Time (US & Canada) (change)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]