继续发帖。。。。

tianyazaixian 2011-12-15 10:40:09
把动态行转列里的结果放到视图或者表里(只要是其他表能引用的就可以了)
测试数据:
create table hl(name char(22),dated datetime,shuliang int)

insert into hl
select 'swz','2011-04-4',12 union all
select 'swz','2011-04-4',23 union all
select 'swz','2011-06-4',12 union all
select 'swz','2011-04-4',12 union all
select 'cl','2011-04-4',134 union all
select 'cl','2011-03-4',134 union all
select 'cl','2011-09-4',134
--动态行转列(要转的结果)
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case cast(datepart(mm,dated) as char(5)) when ''' + cast(datepart(mm,dated) as char(5)) + ''' then shuliang else 0 end) [' + cast(datepart(mm,dated) as char(7)) + ']'
from (select distinct dated from hl) as a
set @sql = @sql + ' from hl group by name'
exec(@sql)

...全文
32 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
tianyazaixian 2011-12-16
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 pengxuan 的回复:]

SQL code

create table hl(name char(22),dated datetime,shuliang int)

insert into hl
select 'swz','2011-04-4',12 union all
select 'swz','2011-04-4',23 union all
select 'swz','2011-06-4',12 union all……
[/Quote]
可以放到表里了 但是跟别的表连接是,如何写T1的列名呢?弄完结贴把
pengxuan 2011-12-15
  • 打赏
  • 举报
回复

create table hl(name char(22),dated datetime,shuliang int)

insert into hl
select 'swz','2011-04-4',12 union all
select 'swz','2011-04-4',23 union all
select 'swz','2011-06-4',12 union all
select 'swz','2011-04-4',12 union all
select 'cl','2011-04-4',134 union all
select 'cl','2011-03-4',134 union all
select 'cl','2011-09-4',134
--动态行转列(要转的结果)
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case cast(datepart(mm,dated) as char(5)) when ''' + cast(datepart(mm,dated) as char(5)) + ''' then shuliang else 0 end) [' + cast(datepart(mm,dated) as char(7)) + ']'
from (select distinct dated from hl) as a
set @sql = @sql + ' into t1 from hl group by name' --在查询里使用into到一个新表中
exec(@sql)
select * from t1 --查询新表
tianyazaixian 2011-12-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 maco_wang 的回复:]

SQL code

create table hl(name char(22),dated datetime,shuliang int)

insert into hl
select 'swz','2011-04-4',12 union all
select 'swz','2011-04-4',23 union all
select 'swz','2011-06-4',12 union all……
[/Quote]

谢谢啊 我想在set @sql = 'select Name as ' + '姓名'加一列统计总量 老是逗号提示错误,怎么弄呢?
--小F-- 2011-12-15
  • 打赏
  • 举报
回复
创建一个临时表

然后insert into #tb exec(@sql)
叶子 2011-12-15
  • 打赏
  • 举报
回复

create table hl(name char(22),dated datetime,shuliang int)

insert into hl
select 'swz','2011-04-4',12 union all
select 'swz','2011-04-4',23 union all
select 'swz','2011-06-4',12 union all
select 'swz','2011-04-4',12 union all
select 'cl','2011-04-4',134 union all
select 'cl','2011-03-4',134 union all
select 'cl','2011-09-4',134
--动态行转列(要转的结果)
declare @sql varchar(8000)
set @sql = 'select Name as ' + '姓名'
select @sql = @sql + ' , max(case cast(datepart(mm,dated) as char(5)) when ''' + cast(datepart(mm,dated) as char(5)) + ''' then shuliang else 0 end) [' + cast(datepart(mm,dated) as char(7)) + ']'
from (select distinct dated from hl) as a
set @sql = @sql + ' into newh1 from hl group by name'
exec(@sql)

select * from newh1

/*
姓名 3 4 6 9
---------------------- ----------- ----------- ----------- -----------
cl 134 134 0 134
swz 0 23 12 0
*/

34,587

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧