存储过程动态给表加列问题

qqyatou 2010-05-06 09:38:20
A表
message time id orgid
123 2010 1 12
456 2010 1 12
789 2010 2 14
963 2010 1 15

我想要的结果如下

orgid message time message1 time1
12 123 2010 456 2010
14 789 2010 null null
15 963 2010 null null

也就是说把所有orgid一样的放一行,但是每个orgid的数据是不一定一样的,这个表有多少列也不是一定的

不知道说清楚了没有,给点思路,以前写的都是固定有多少列的,动态的??如何写
...全文
181 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqyatou 2010-05-06
  • 打赏
  • 举报
回复
小F不好意思哦 结的时候没看到你的回复
谢谢你
--小F-- 2010-05-06
  • 打赏
  • 举报
回复
declare @sql varchar(8000)
set @sql = 'select orgid '
select
@sql = @sql + ' ,
max(case px when ''' + ltrim(px) + ''' then message else 0 end) [' + ltrim(message) + '],
max(case px when ''' + ltrim(px) + ''' then time else 0 end) [' + ltrim(time) + ']'
from (select px=row_number()over(partition by orgid order by getdate()),* from a)a
set @sql = @sql + ' from (select px=row_number()over(partition by orgid order by getdate()),* from a)a group by orgid'
exec(@sql)
黄_瓜 2010-05-06
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 ws_hgo 的回复:]
SQL code
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert i……
[/Quote]
up
qqyatou 2010-05-06
  • 打赏
  • 举报
回复
谢谢了 我试下
ws_hgo 2010-05-06
  • 打赏
  • 举报
回复
create table tb(姓名 varchar(10) , 课程 varchar(10) , 分数 int)
insert into tb values('张三' , '语文' , 74)
insert into tb values('张三' , '数学' , 83)
insert into tb values('张三' , '物理' , 93)
insert into tb values('李四' , '语文' , 74)
insert into tb values('李四' , '数学' , 84)
insert into tb values('李四' , '物理' , 94)


select UserName,sum(case when Subject= '数学' then Score else 0 end) [数学],sum(case when Subject= '物理' then Score else 0 end) [物理],sum(case when Subject= '语文' then Score else 0 end) [语文]
declare @sql varchar(1000)
set @sql='select UserName'
select @sql=@sql+',sum(case when Subject= ''' +Subject+ ''' then Score else 0 end) ['+Subject+']' from (select distinct Subject from tb)a

set @sql = @sql + ' from tb group by UserName'
print @sql
exec(@sql)


--讲解:

--这个是第一次执行
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学],
--这个是第二次
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] ,
--这个是第三次
select 姓名,max(case 课程 when '数学' then 分数 else 0 end) [数学], max(case 课程 when '物理' then 分数 else 0 end) [物理] , max(case 课程 when '语文' then 分数 else 0 end) [语文]
--这个的数量来自于
(select distinct 课程 from tb)--这里只有3们课程



create table tb(id int, value varchar(10))
insert into tb values(1, 'aa')
insert into tb values(1, 'bb')
insert into tb values(2, 'aaa')
insert into tb values(2, 'bbb')
insert into tb values(2, 'ccc')
go

create function dbo.f_str(@id int) returns varchar(100)
as
begin
declare @str varchar(1000)
set @str = ''
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
set @str = right(@str , len(@str) - 1)
return @str
end
go

--调用函数
select id , value = dbo.f_str(id) from tb group by id

drop function dbo.f_str
drop table tb


我只说一个地方
select @str = @str + ',' + cast(value as varchar) from tb where id = @id
你把这个看懂就明白了
例如当@id=1
select @str = @str + ',' + cast(value as varchar) from tb where id = 1
把满足id=1的str值通过','累加
当id是动态的就是1或者2...是当满足1的查询完了,把值付给str之后
在查询满足2的直到所有的ID完为止
这样明白了吧




本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ws_hgo/archive/2009/03/17/3999394.aspx
qqyatou 2010-05-06
  • 打赏
  • 举报
回复
怎么还带去空格的,表结构都没了

34,590

社区成员

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

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