oracle sql not a group by expression while countin

2019-03-01 11:56发布

This question already has an answer here:

i'm trying to run the following query that counts the number of doctors with patient referrals within the last 6 months:

SELECT 
    count(local_doc.providerno) NumberofReferals, 
    LOCAL_DOC.FULLNAME FULLNAME, 
    LOCAL_DOC.TELNUMBER TELNUMBER, 
    LOCAL_DOC.STREET STREET, 
    LOCAL_DOC.SUBURB SUBURB, 
    LOCAL_DOC.STATE STATE, 
    LOCAL_DOC.POSTCODE POSTCODE,
    LOCAL_DOC.PROVIDERNO
FROM 
    LOCAL_DOC, 
    PATIENT 
WHERE 
    PATIENT.PROVIDERNO = LOCAL_DOC.PROVIDERNO AND PATIENT.REFERALDATE <= sysdate AND PATIENT.REFERALDATE >= ADD_MONTHS(sysdate, -6)
    group by local_doc.providerno;

i keep getting

ORA-00979: not a GROUP BY expression
00979. 00000 -  "not a GROUP BY expression"
*Cause:    
*Action:
Error at Line: 3 Column: 5

and have no idea how to get around this. How could I do so?

3条回答
欢心
2楼-- · 2019-03-01 12:33

add

GROUP BY 
    LOCAL_DOC.FULLNAME , 
    LOCAL_DOC.TELNUMBER , 
    LOCAL_DOC.STREET , 
    LOCAL_DOC.SUBURB , 
    LOCAL_DOC.STATE , 
    LOCAL_DOC.POSTCODE ,
    LOCAL_DOC.PROVIDERNO

Most DBS other than MYSql will require you to group on the remaining selected columns

查看更多
走好不送
3楼-- · 2019-03-01 12:36

Dear, group By Clause ask a user to specify all Select Columns in Group By Clause as well except select columns with aggregated functions.

Look in my Group by Clause i have specify all columns in Select except local_doc.providerno because it is using Aggregated function COUNT

For better understanding please refer to this link

 SELECT 
    count(local_doc.providerno) NumberofReferals, 
    LOCAL_DOC.FULLNAME FULLNAME, 
    LOCAL_DOC.TELNUMBER TELNUMBER, 
    LOCAL_DOC.STREET STREET, 
    LOCAL_DOC.SUBURB SUBURB, 
    LOCAL_DOC.STATE STATE, 
    LOCAL_DOC.POSTCODE POSTCODE,
    LOCAL_DOC.PROVIDERNO
FROM 
    LOCAL_DOC, 
    PATIENT 
WHERE 
    PATIENT.PROVIDERNO = LOCAL_DOC.PROVIDERNO AND PATIENT.REFERALDATE <= sysdate AND PATIENT.REFERALDATE >= ADD_MONTHS(sysdate, -6)
GROUP BY
    LOCAL_DOC.FULLNAME , 
    LOCAL_DOC.TELNUMBER , 
    LOCAL_DOC.STREET , 
    LOCAL_DOC.SUBURB , 
    LOCAL_DOC.STATE , 
    LOCAL_DOC.POSTCODE ,
    LOCAL_DOC.PROVIDERNO
查看更多
别忘想泡老子
4楼-- · 2019-03-01 12:36

in group by you must know which columns can got repeated value in it rows and then add it to your group by the query is:

SELECT 
count(local_doc.providerno) NumberofReferals, 
LOCAL_DOC.FULLNAME FULLNAME, 
LOCAL_DOC.TELNUMBER TELNUMBER, 
LOCAL_DOC.STREET STREET, 
LOCAL_DOC.SUBURB SUBURB, 
LOCAL_DOC.STATE STATE, 
LOCAL_DOC.POSTCODE POSTCODE,
LOCAL_DOC.PROVIDERNO
FROM 
LOCAL_DOC, 
PATIENT 
WHERE 
PATIENT.PROVIDERNO = LOCAL_DOC.PROVIDERNO AND PATIENT.REFERALDATE <= sysdate AND PATIENT.REFERALDATE >= ADD_MONTHS(sysdate, -6)
group by 
LOCAL_DOC.FULLNAME, 
LOCAL_DOC.TELNUMBER, 
LOCAL_DOC.STREET, 
LOCAL_DOC.SUBURB, 
LOCAL_DOC.STATE, 
LOCAL_DOC.POSTCODE,
LOCAL_DOC.PROVIDERNO;
查看更多
登录 后发表回答