sql问题(50分)

S

sqz

Unregistered / Unconfirmed
GUEST, unregistred user!
有 一sql server数据库中TABLE1字段为id(编号),sj(内容),rq(日期)现要汇总统计
每个id,每个rq中sj的所有内容。sj为char
如:id sj rq
0101 1111 2002-10-20
0102 2222 2002-10-20
0101 2222 2002-10-20
0101 4444 2002-10-21
结果:
0101 11112222 2002-10-20
0102 2222 2002-10-20
0101 4444 2002-10-21

在线等待!立即给分
 
select table1.id,a.sj
from table1 ,(select count(sj)as allsj from table1 as a)
where table1.id=a.id
试试看。。。
我也不知道行不行。
 
更正一下。
select table1.id,a.allsj
from table1 ,(select count(sj)as allsj from table1 as a)
where table1.id=a.id
 
to:astart
你的肯定不对呀!
 
你看看游标怎么使得,我想用游标是可以实现的。
 
不用游标可以吗?
 
declare @tmptb1 table (nid int, sj varchar(500), rq datetime)
insert into @tmptb1 (nid, rq) select distinct [id], rq from table1
update @tmptb1 set sj=isnull(a.sj,'')+b.sj
from @tmptb1 a inner join table1 b on a.nid=b.[id] and a.rq=b.rq
select * from @tmptb1

试一下吧,我也不能肯定行。
 
还是不行呀!
 
CREATE PROCEDURE temp1 AS
Begin
Select id,rq,sj,identity(int,1,1) as tmp
Into #temp1
From table1 order by id,rq

Select id,rq,sj,(select count(tmp)+1 from #temp1 where id=AA.id and rq=AA.rq and tmp<AA.tmp) as tmp
Into #temp2
From #temp1 AA

Select distinct id,rq,space(255) as sj
Into #temp3
From table1
order by id,rq

Declare @tmp int
Set @tmp=1

While @tmp<=(select max(tmp) from #temp2)
Begin
Update #temp3
Set sj=rtrim(AA.sj)+rtrim(BB.sj)
From #temp3 AA,#temp2 BB
Where AA.id=BB.id and AA.rq=BB.rq and BB.tmp=@tmp

Set @tmp=@tmp+1
end

Select id,sj,rq From #temp3
End
GO
 
QuickSilver 不愧为sql高手,佩服佩服!
 
QuickSilver:高手!!!!!
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
987
SUNSTONE的Delphi笔记
S
S
回复
0
查看
805
SUNSTONE的Delphi笔记
S
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
764
import
I
顶部