求一视图脚本 请教高手

来源:百度知道 编辑:UC知道 时间:2024/09/28 09:23:04
实现这样
nd deptid xmid money
2008 001000000 j001 220.00
2008 001060000 g004 65.00
2008 001060000 j006 45.00
2008 001060000 j001 6.00//这个加下面的
2008 001060000 j00101 34.00
如果xmid的前四位、deptid、nd相同的则sum(money)
上表实现这样的
nd deptid xmid money
2008 001000000 j001 220.00
2008 001060000 g004 65.00
2008 001060000 j006 45.00
2008 001060000 j001 40.00//变成这个
2008 001060000 j00101 34.00

数据库是sybase11的 不能union all .....
MSSQL的有了 可是在SYBASE11里面不行

数据库是没希望换了...只能用它了...

136601113 你不懂吧...
你那个简单的例子还用研究哇...

你能实现我说的功能算你狠!
拜托要发表意见的时候先了解一下...我资料写的很全了...

create view viewtest
as
select a.*,b.money from
(select distinct nd,deptid,substring(xmid,1,4) xmid from T1 )a left join
(select nd,deptid,substring(xmid,1,4) xmid, sum(money) as money
from T1
group by nd,deptid,substring(xmid,1,4))b
on a.nd=b.nd and a.deptid=b.deptid and a.xmid =b.xmid

不知道可以么,试一下
或者

select distinct a.nd,a.deptid,case when a.money=b.money then a.xmid
else b.xmid end xmid ,b.money from
(select nd,deptid,xmid,money from #t1 )a ,
(select nd,deptid,substring(xmid,1,4) xmid, sum(money) as money
from #t1
group by nd,deptid,substring(xmid,1,4))b
where a.nd=b.nd and a.deptid=b.deptid and substring(a.xmid,1,4) =b.xmid

不过,如果你的意思是
2008 001060000 j001 40.00//变成这个
2008 001060000 j00101 34.00
下面还有的话,只有存储过程可以用循环实现吧

唉,估计又是大学生,自己老不动脑筋.发个例子你自己研究.多学习.
/*-------- 发帖事务成功演示 --------*/

begin transacti