日期:2014-05-17  浏览次数:20795 次

遇到一个sql问题,求助
表a:userid,username
表b:userid,incost,
表c:userid,outcost
现在要查出这样一个结果集:userid ,username,sum(incost),sum(outcost)
我的sql如下:
select a.userid,(a.username),sum(b.incost) as sumincost,sum(c.outcost) as sumoutcost 
from xxzx_user_zyl a,xxzx_incost b, xxzx_outcost c
where a.userid=b.userid and b.userid=c.userid 
group by a.userid,a.username

结果查出来的结果总是不对,求高人指导

------解决方案--------------------
userid是否有重复的,
这个应该用full join 吧
------解决方案--------------------
SQL code

select a.userid,
       a.username,
       sum(b.incost) sumincost,
       sum(c.outcost) sumoutcost  
from xxzx_user_zyl a,xxzx_incost b, xxzx_outcost c
where a.userid=b.userid 
      and b.userid=c.userid  
group by a.userid,a.username

------解决方案--------------------
SQL code


SELECT userid,username,Sum(incost) over (PARTITION BY userid,username) as incost,Sum(outcost) over (PARTITION BY userid,username) as outcost FROM 
xxzx_user_zyl a,xxzx_incost b, xxzx_outcost c

WHERE a.userid=b.userid(+)
AND a.userid=c.userid(+)

------解决方案--------------------
select a.userid, a.username, sum(b.incost) sumincost, sum(c.outcost) sumoutcost from xxzx_user_zyl a,xxzx_incost b, xxzx_outcost c where a.userid=b.userid(+) and A.userid=c.userid(+) group by a.userid,a.username
是以A表为准,进行外关联