冰天雪地,裸体跪求:sql语句的写法|?????

dashangg 2004-01-10 08:59:53
bmbh fylb fyje
003 001 1400.00
004 001 100
003 002 12300
003 003 654
004 003 456
005 003 56
003 005 4564
上面为表内容,要求SQL语句达到的效果是把上面的数据插到新表中,如下
bmbh fylb001 fylb002 fylb003 fylb005........
003 1440.00 123.00 654 45664
004 100 654
005 56
...全文
29 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmcao 2004-01-11
  • 打赏
  • 举报
回复
建哥好恐怖!应该给分!!支持!
gmlxf 2004-01-11
  • 打赏
  • 举报
回复
select @s=@s+',['+fylb+']=sum(case fylb when '''+fylb+''' then fyje end)'
from (select distinct fylb from 表) a
print @s -- 注意这里,你打印出来,帖到上面慢慢体会就知道了。
exec('select bmbh'+@s+' from 表 group by bmbh')
ken2002 2004-01-11
  • 打赏
  • 举报
回复
from (select distinct fylb from 表) a

为什么要上面这一句呢,能说说这句话的功能么,多谢了!
hglhyy 2004-01-11
  • 打赏
  • 举报
回复
楼上的很好!
sql1 2004-01-11
  • 打赏
  • 举报
回复
看 马可的!吧
zjcxc 元老 2004-01-10
  • 打赏
  • 举报
回复
create table 表(bmbh varchar(3),fylb varchar(3),fyje float)
insert into 表
select '003','001',1400.00
union all select '004','001',100
union all select '003','002',12300
union all select '003','003',654
union all select '004','003',456
union all select '005','003',56
union all select '003','005',4564
go

declare @s varchar(8000)
set @s=''
select @s=@s+',[fylb'+fylb+']=sum(case fylb when '''+fylb+''' then fyje end)'
from (select distinct fylb from 表) a
exec('select bmbh'+@s+' from 表 group by bmbh')
go

drop table 表

/*--测试结果
bmbh fylb001 fylb002 fylb003 fylb005
---- ---------- ---------- ---------- --------
003 1400.0 12300.0 654.0 4564.0
004 100.0 NULL 456.0 NULL
005 NULL NULL 56.0 NULL

--*/
zjcxc 元老 2004-01-10
  • 打赏
  • 举报
回复
create table 表(bmbh varchar(3),fylb varchar(3),fyje float)
insert into 表
select '003','001',1400.00
union all select '004','001',100
union all select '003','002',12300
union all select '003','003',654
union all select '004','003',456
union all select '005','003',56
union all select '003','005',4564
go

declare @s varchar(8000)
set @s=''
select @s=@s+',['+fylb+']=sum(case fylb when '''+fylb+''' then fyje end)'
from (select distinct fylb from 表) a
exec('select bmbh'+@s+' from 表 group by bmbh')
go

drop table 表

/*--测试结果
bmbh 001 002 003 005
---- -------- -------- -------- -------
003 1400.0 12300.0 654.0 4564.0
004 100.0 NULL 456.0 NULL
005 NULL NULL 56.0 NULL

--*/
wzh1215 2004-01-10
  • 打赏
  • 举报
回复
--马可的!
http://expert.csdn.net/Expert/topic/2440/2440306.xml?temp=.660351
realgz 2004-01-10
  • 打赏
  • 举报
回复
又是
group by +case
haipingma 2004-01-10
  • 打赏
  • 举报
回复
up
Jianli2004 2004-01-10
  • 打赏
  • 举报
回复
小心着凉

34,590

社区成员

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

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