qinyan
asked on
Dynamic way to turn rows (which are already in cross-tab format) into columns?
The recordset I have is like this:
Date Type North_Cnt West_Cnt
5/1/09 A 20 30
5/1/09 B 40 25
5/2/09 A 30 20
5/2/09 B 35 45
...
I want to turn it into this way:
Date Type North_Cnt West_Cnt Date Type North_Cnt West_Cnt
5/1/09 A 20 30 5/2/09 A 30 20
5/1/09 B 40 25 5/2/09 B 35 45
so if there are 10 dates then there will be 10 sections each of which has 4 columns (date, type, North_cnt, West_cnt) so totally 40 columns.
I want to try Pivot in 2005 but doubt it would work. I guess the only way is to use dynamic sql?
Has anyone done something similar like this before?
Date Type North_Cnt West_Cnt
5/1/09 A 20 30
5/1/09 B 40 25
5/2/09 A 30 20
5/2/09 B 35 45
...
I want to turn it into this way:
Date Type North_Cnt West_Cnt Date Type North_Cnt West_Cnt
5/1/09 A 20 30 5/2/09 A 30 20
5/1/09 B 40 25 5/2/09 B 35 45
so if there are 10 dates then there will be 10 sections each of which has 4 columns (date, type, North_cnt, West_cnt) so totally 40 columns.
I want to try Pivot in 2005 but doubt it would work. I guess the only way is to use dynamic sql?
Has anyone done something similar like this before?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I haven't worked with PIVOT a lot, but I'll assume that "sum(Tcs_quantity) and sum(tcs_quantity)" are acutally two different columns. You MAY need to do two pivot statements. Or two whole selects, built as derived tables, then joined together.
ASKER
actually since my recordset is already in pivot format i'm looking for something like this:
select * from
(
select * from myTable
where date=5/1/09'
) a
join
(
select * from myTable
where date=5/2/09'
) b
on a.type=b.type
and make it dynamic:
declare @min datetime
select @min = min(date) from myTable
declare @max datetime
select @max=max(date) from myTable
declare @sql varchar(max)
select @sql = 'SELECT * from (select * from myTable where date=, ' + CHAR(13),
while @min<=@max
begin
SET @sql = @sql + ''
SET @min = @min + 7 ;
end
select * from
(
select * from myTable
where date=5/1/09'
) a
join
(
select * from myTable
where date=5/2/09'
) b
on a.type=b.type
and make it dynamic:
declare @min datetime
select @min = min(date) from myTable
declare @max datetime
select @max=max(date) from myTable
declare @sql varchar(max)
select @sql = 'SELECT * from (select * from myTable where date=, ' + CHAR(13),
while @min<=@max
begin
SET @sql = @sql + ''
SET @min = @min + 7 ;
end
ASKER
I haven't worked with PIVOT a lot, but I'll assume that "sum(Tcs_quantity) and sum(tcs_quantity)" are acutally two different columns. You MAY need to do two pivot statements. Or two whole selects, built as derived tables, then joined together.
Yes I can do it that way and that's the only way to do it if using pivot, but I was just trying to avoid two or multiple pivot statements as I have 4 columns which i need to do aggregation on. That's why i was thinking about other ways in dynamic sql.
Thanks!
Yes I can do it that way and that's the only way to do it if using pivot, but I was just trying to avoid two or multiple pivot statements as I have 4 columns which i need to do aggregation on. That's why i was thinking about other ways in dynamic sql.
Thanks!
ASKER