1

我正在尝试运行以下查询来计算过去 6 个月内有患者转诊的医生人数:

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;

我不断得到

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

并且不知道如何解决这个问题。我怎么能这样做?

4

3 回答 3

3

亲爱的, group By Clause 要求用户在 Group By Clause 中指定所有 Select Columns,除了具有聚合函数的 select 列。

查看我的 Group by Clause 我已经在 Select 中指定了除 local_doc.providerno之外的所有列,因为它使用的是聚合函数 COUNT

为了更好地理解,请参阅此链接

 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
于 2013-10-07T04:40:25.413 回答
0

在 group by 你必须知道哪些列可以在它的行中获得重复值,然后将其添加到你group by的查询是:

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;
于 2013-10-07T07:14:57.747 回答
0

添加

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

MYSql 以外的大多数 DBS​​ 将要求您对剩余的选定列进行分组

于 2013-10-07T04:39:31.037 回答