sql 拼接问题。在线等。。。。

fengyuananan 2011-08-12 10:47:23
select * from Forbug_HTMZje 这就话得到的集合是
id xmdc xmsj xmje
1 001014 2011-4-1 0:00:00 1000000.0000
2 001014 2011-5-1 0:00:00 10000.0000
3 001013 2011-5-1 0:00:00 450000.0000
4 001013 2011-4-1 0:00:00 100000.0000
5 001012 2011-4-1 0:00:00 10000500.0000
6 001014 2011-7-1 0:00:00 1200000.0000
select * from Forbug_HTMHkZje 这就话得到的集合是
id hkdc hksj hkje
1 001014 2011-4-1 0:00:00 30000.0000
2 001014 2011-5-1 0:00:00 5000.0000
3 001013 2011-4-1 0:00:00 55555.0000
我现在想得到这样一个组合:
(时间是以第一个表为准) (这三列是xmdc) (这两列是hkdc)
time 001012 001013 001014 001013 001014 --这一行是标题
2011-4 10000500 100000 1000000 55555 30000
2011-5 0 450000 10000 0 5000
2011-7 0 0 1200000 0 0

sql怎么写才能得到这个集合呢?请高手帮我看看。。。。
...全文
103 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liang145 2011-08-12
  • 打赏
  • 举报
回复

create table #Forbug_HTMZje
(id int,xmdc nvarchar(10),xmsj datetime,xmje dec(18,2))
insert #Forbug_HTMZje
select 1,'001014','2011-4-1 0:00:00',1000000.0000 union all
select 2,'001014','2011-5-1 0:00:00',10000.0000 union all
select 3,'001013','2011-5-1 0:00:00',450000.0000 union all
select 4,'001013','2011-4-1 0:00:00',100000.0000 union all
select 5,'001012','2011-4-1 0:00:00',10000500.0000 union all
select 6,'001014','2011-7-1 0:00:00',1200000.0000

create table #Forbug_HTMHkZje
(id int,hkdc nvarchar(10),hksj datetime,hkje dec(18,2))
insert #Forbug_HTMHkZje
select 1,'001014','2011-4-1 0:00:00',30000.0000 union all
select 2,'001014','2011-5-1 0:00:00',5000.0000 union all
select 3,'001013','2011-4-1 0:00:00',55555.0000

declare @str nvarchar(4000)
set @str='select xmsj as [time]'
select @str=@str+',max(case when xmdc='''+xmdc+''' then xmje else 0 end) as '''+xmdc+''''
from (select distinct xmdc from #Forbug_HTMZje) as a
set @str=@str+' from #Forbug_HTMZje group by xmsj'

declare @str2 nvarchar(4000)
set @str2='select hksj as [time]'
select @str2=@str2+',max(case when hkdc='''+hkdc+''' then hkje else 0 end) as '''+hkdc+''''
from (select distinct hkdc from #Forbug_HTMHkZje) as a
set @str2=@str2+' from #Forbug_HTMHkZje group by hksj'

declare @str3 nvarchar(4000)
set @str3='select * from ('+@str+') as a Left join ('+@str2+') as b on a.time=b.time'
exec(@str3)

--time 001012 001013 001014 time 001013 001014
------------------------- ------------- ------------- -------------- ----------------------- ----------------------------
--2011-04-01 00:00:00.000 10000500.00 100000.00 1000000.00 2011-04-01 00:00:00.000 55555.00 30000.00
--2011-05-01 00:00:00.000 0.00 450000.00 10000.00 2011-05-01 00:00:00.000 0.00 5000.00
--2011-07-01 00:00:00.000 0.00 0.00 1200000.00 NULL NULL NULL
geniuswjt 2011-08-12
  • 打赏
  • 举报
回复
又见列转行- -
fengyuananan 2011-08-12
  • 打赏
  • 举报
回复
能帮我看看吗,我还是不会写sql,你那个问题,是不是算1 2 3 4 的个数啊。。。。
没太看懂转换的规律。。。
一缕青烟 2011-08-12
  • 打赏
  • 举报
回复
先 UNION ALL 一下,就和我问的问题差不多了。


列值转元组
fty920 2011-08-12
  • 打赏
  • 举报
回复
打酱油
fengyuananan 2011-08-12
  • 打赏
  • 举报
回复
insert into Forbug_HTMZje values('001001','2011-1-1',2000000)
insert into Forbug_HTMHkZje values('001001','2011-1-1',20000)
这样的数据就可以啊。。。上面那些也行。。。
快溜 2011-08-12
  • 打赏
  • 举报
回复
给点测试数据啊。
fengyuananan 2011-08-12
  • 打赏
  • 举报
回复
这样写,好像报错啊。。
消息 137,级别 15,状态 2,第 2 行
必须声明标量变量 "@str"。
而且下面的三句还必须分别运行,,,

34,590

社区成员

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

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