declare @i int
declare @s varchar(300)
set @i = 1
set @s = 'select ImlId as 月份'
while exists (select * from xt_color where cast(xtColorID as int) = @i )
begin
select @s = @s + ',(case when cast(imlcolor as int) ='+ cast(@i as varchar) + ' then
imlnum else 0 end) as ' +xtColorName from xt_color where cast(xtColorID as int) = @i
set @i = @i + 1
end
set @s = @s + ' from gc_importlist order by ImlId'
exec (@s)
出错
declare @i int
declare @s varchar(300)
set @i =2
set @s = 'select ImlId as 月份'
while exists (select * from xt_color where cast(xtColorID as int) = @i )
begin
select @s = @s + ',(case when cast(imlcolor as int) ='+ cast(@i as varchar) + ' then
imlnum else 0 end) as ' +xtColorName from xt_color where cast(xtColorID as int) = @i
set @i = @i + 1
end
set @s = @s + ' from gc_importlist order by ImlId'
exec (@s)
这是从MS的例子交叉表改的,不知道正确吗
SELECT size,
(CASE color WHEN 1 THEN Amount ELSE 0 END) AS 红,
(CASE color WHEN 2 THEN Amount ELSE 0 END) AS 蓝,
(CASE color WHEN 3 THEN Amount ELSE 0 END) AS 黄,