运行查询时,“ORA-00937:不是单组组功能”

时间:2014-08-29 10:36:48

标签: sql group-by oracle10g

我正在使用oracle 10g。我的疑问是:

select "Debtor"."DebtorName",
sum(case when regionTable."Text" like '%North%' 
then to_number(count("Registration"."pkRegistrationId")) else 0 end) "North",
 sum(case when regionTable."Text" like '%South%' 
then to_number(count("Registration"."pkRegistrationId")) else 0 end) "South"
from "Registration"
inner join "CustomerRequisition" on "CustomerRequisition"."pkCustomerRequisitionId"="Registration"."fkCustomerRequisitionId"
inner join "EnumerationValue" regionTable on regionTable."pkEnumerationValueId"="CustomerRequisition"."fkRegion"
inner join "Debtor" on "Debtor"."pkDebtorId"="CustomerRequisition"."fkDebtorId"
group by "Debtor"."DebtorName",regionTable."Text","Registration"."pkRegistrationId"

我收到了这个错误。

ORA-00937: not a single-group group function

我在这里缺少什么?

1 个答案:

答案 0 :(得分:2)

这是您的查询:

select "Debtor"."DebtorName",
       sum(case when regionTable."Text" like '%North%' 
                then to_number(count("Registration"."pkRegistrationId")) else 0
           end) "North",
       sum(case when regionTable."Text" like '%South%' 
                then to_number(count("Registration"."pkRegistrationId")) else 0
           end) "South"
from "Registration" inner join
     "CustomerRequisition
     on "CustomerRequisition"."pkCustomerRequisitionId" = "Registration"."fkCustomerRequisitionId" inner join
     "EnumerationValue" regionTable    
     on regionTable."pkEnumerationValueId"="CustomerRequisition"."fkRegion" inner join
     "Debtor"
     on "Debtor"."pkDebtorId"="CustomerRequisition"."fkDebtorId"
group by "Debtor"."DebtorName", regionTable."Text", "Registration"."pkRegistrationId";

您有嵌套聚合函数。虽然Oracle确实允许这样做,但我怀疑你是否真的想要这个功能。我猜你真的想要这样的东西:

select "Debtor"."DebtorName",
       sum(case when regionTable."Text" like '%North%' 
                then 1 else 0
           end) "North",
       sum(case when regionTable."Text" like '%South%' 
                then 1 else 0
           end) "South"
from "Registration" inner join
     "CustomerRequisition"
     on "CustomerRequisition"."pkCustomerRequisitionId" = "Registration"."fkCustomerRequisitionId" inner join
     "EnumerationValue" regionTable    
     on regionTable."pkEnumerationValueId" = "CustomerRequisition"."fkRegion" inner join
     "Debtor"
     on "Debtor"."pkDebtorId" = "CustomerRequisition"."fkDebtorId"
group by "Debtor"."DebtorName";

您可能需要count(distinct)代替:

select "Debtor"."DebtorName",
       count(distinct case when regionTable."Text" like '%North%' 
                      then "Registration"."pkRegistrationId"
           end) "North",
       count(distinct case when regionTable."Text" like '%South%' 
                      then "Registration"."pkRegistrationId"
           end) "South"