有没有人会写这条sql语句?

qozm 2003-09-14 04:34:27
我数据库中有记录为
ID Num Value
1 111,222,333 一一一,二二二,三三三
1 222,333 二二二,三三三

我要用sql语句把它分成如下
1 111 一一一
2 222 二二二
3 333 三三三
4 222 二二二
5 333 三三三

这条sql语句该怎么写?

不要使用游标,效益太低了,我表中有几万条这样的记录
...全文
21 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
laurecn 2003-09-15
  • 打赏
  • 举报
回复
you can find some method at:
http://expert.csdn.net/Expert/topic/2147/2147995.xml?temp=.6963312
qozm 2003-09-15
  • 打赏
  • 举报
回复
位数是一致的吗?都是111,一一一这样的对应吗
---------

不一定,也有些是
Num Value
12,23 啊啊啊,塞额达的
3332,23 啊额的,塞额达的
2 的可

毫无规则的数据,但是每一个Num肯定有一个Value对应
lfengxu 2003-09-15
  • 打赏
  • 举报
回复
declare @id varchar(1000)

declare @string varchar(1000)
set @string='select identity(int,1,1) as id,* into ##tmp from (select '''+replace(@id,',','''as purc_need_id union all select ''')+''') a'
exec(@string)
select a.prod_id,b.purc_need_id,a.supp_id into #aaa
from purchase_prod a,purchase b
where a.purc_need_id=b.purc_need_id and b.purc_need_id
in (select c.purc_need_id from ##tmp c,purchase d where c.purc_need_id=d.purc_need_id ) order by prod_id
select a.prod_id,a.prod_name,a.prod_type,a.purc_proc_quan,a.purc_proc_total_price, 0 quan, cast('' as varchar(500)) as value into #ccc
from purchase_prod a,#aaa b
where a.prod_id=b.prod_id and a.purc_need_id=b.purc_need_id and a.supp_id=b.supp_id
--select * from #aaa

select prod_id,prod_name,prod_type,sum(purc_proc_quan) as quan,sum(purc_proc_total_price) as total from #ccc group by prod_id,prod_name,prod_type
--select max(prod_id) as prod_id,max(value) result into #ddd from #ccc group by quan
--select * from #ccc

--select * from #ccc order by prod_id
--select sum(purc_proc_quan) as quan1 ,sum(purc_proc_total_price) as total from #ccc where prod_id=21
drop table ##tmp
drop table #aaa
drop table #ccc
yckou 2003-09-15
  • 打赏
  • 举报
回复
问一下,位数是一致的吗?都是111,一一一这样的对应吗?
qozm 2003-09-14
  • 打赏
  • 举报
回复
好我重新发了一下,100分
yujohny 2003-09-14
  • 打赏
  • 举报
回复
楼主,你干吗不一个问题发一个100分的贴,我想这个问题的难度值100分,要不回答的人恐怕没什么动力,因为时间跟分不成比例
OpenVMS 2003-09-14
  • 打赏
  • 举报
回复
如果逗号个数不定,不用游标难弄

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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