日期:2014-05-16  浏览次数:20791 次

关于oracle中的group by
create or replace view v_member_inspection_sum(person_id,user_name,inspectorDeviceNum) as select  userinfo.person_id,userinfo.user_name,count(devicebaseinfo.deviceID) from userinfo,devicebaseinfo where userinfo.person_id = devicebaseinfo.inspectionPersonID(+) and  userinfo.deleteFlag<>-1 and devicebaseinfo.deleteFlag<>-1 group by userinfo.user_name,userinfo.person_id;



上面的语句可以查询出person_id,user_name,但是只能查询出count(devicebaseinfo.deviceID)大于0的person_id,user_name,我现在要的效果是查询全部的person_id,不管count(devicebaseinfo.deviceID)是否大于0,请问大神我该怎么改?
------解决方案--------------------
select userinfo.person_id,
       userinfo.user_name,
       count(devicebaseinfo.deviceID)
  from userinfo, devicebaseinfo
 where userinfo.person_id = devicebaseinfo.inspectionPersonID(+)
   and userinfo.deleteFlag <> -1
   and devicebaseinfo.deleteFlag <> -1
 group by userinfo.user_name,userinfo.person_id;

红色这段条件限制deleteFlag!=-1.
假如你只是要连接查询统计的话,可以修改为:

and devicebaseinfo.deleteFlag(+) <> -1