日期:2014-05-18  浏览次数:20436 次

SQL查询问题~~急~~group by 出了问题~~
语句是这样的
select   medicode,mediname,spec,opername,unitpric,sum(quantity)   as   shuliang,sum(itemamou)   as   jin
from   dictoper,dictmedi,nclinreciinfo,nclininvoinfo
where   nclinreciinfo.mediid=dictmedi.mediid   and   nclininvoinfo.doctid=dictoper.operid
            and   nclininvoinfo.autonumb=nclinreciinfo.autonumb   and   nclininvoinfo.ownamou     not   IN
                      (SELECT   abs(ownamou)   as   ownamou
                  FROM   nClinInvoInfo
                  WHERE   status   =   2)
            and   dictoper.opername=:l_opername
            and   nclininvoinfo.strcreadate   between   :d_begin   and   :d_end
group   by   medicode,mediname,spec,opername,unitpric
order   by   medicode   asc

但是执行之后的结果仍然有同样的   medicode   出现在查询结果中
这是为什么,要想避免这个问题,应该怎么改?

------解决方案--------------------
试试:
select medicode,mediname,sum(quantity) as shuliang,sum(itemamou) as jin
from dictoper,dictmedi,nclinreciinfo,nclininvoinfo
where nclinreciinfo.mediid=dictmedi.mediid and nclininvoinfo.doctid=dictoper.operid
and nclininvoinfo.autonumb=nclinreciinfo.autonumb and nclininvoinfo.ownamou not IN
(SELECT abs(ownamou) as ownamou
FROM nClinInvoInfo
WHERE status = 2)
and dictoper.opername=:l_opername
and nclininvoinfo.strcreadate between :d_begin and :d_end
group by medicode,mediname
order by medicode asc

------解决方案--------------------
试一下
select medicode,max(mediname),max(spec),max(opername),max(unitpric),sum(quantity) as shuliang,sum(itemamou) as jin
from dictoper,dictmedi,nclinreciinfo,nclininvoinfo
where nclinreciinfo.mediid=dictmedi.mediid and nclininvoinfo.doctid=dictoper.operid
and nclininvoinfo.autonumb=nclinreciinfo.autonumb and nclininvoinfo.ownamou not IN
(SELECT abs(ownamou) as ownamou
FROM nClinInvoInfo
WHERE status = 2)
and dictoper.opername=:l_opername
and nclininvoinfo.strcreadate between :d_begin and :d_end
group by medicode
order by medicode asc