ORA-00936:在oracle上缺少表达式

时间:2014-09-06 06:43:25

标签: sql oracle10g distinct

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

select DISTINCT ON("Rental"."pkRentalId") "pkRentalId",
    to_number("Reservation"."ReservationNo") "ReservationNo",
    to_char("Rental"."RentalNo") "RentalNo",
    to_char(segmentTable."Text"||'-'||rateTypeTable."Text") "RateType",
    to_char("Debtor"."DebtorName") "DebtorName",
    to_char("Rates"."RateName") "RateName", 
    to_char("Renter"."FirstName"||' '||"Renter"."LastName") "Renter",
    to_date("Rates"."ValidFrom") "ValidFrom",
    to_date("Rates"."ValidTo") "ValidTo",
    round(to_number(to_number("ReservationDuration"."ExpectedCheckinDateTime"-"ReservationDuration"."DispatchDateTime") *to_number("Rates"."UnitRate")),2) "RentalAmount",
    to_number("Deductions"."Amount") "Deduction",
    to_number("Billing"."DiscountAmount") "Discount"
    from "Reservation" 
    inner join "Rental" on "Rental"."pkRentalId"="Reservation"."fkRentalId"
    inner join "Rates" on "Rates"."pkRateId"="Reservation"."fkRateId"
    inner join "Renter" on "Renter"."fkReservationId"="Reservation"."pkReservationId"
    inner join "Billing" on "Billing"."pkBillingId"="Reservation"."fkBillingId"
    inner join "Deductions" on "Deductions"."fkRentalId"="Rental"."pkRentalId"
    inner join "Debtor" on "Debtor"."pkDebtorId"="Rates"."fkDebtorId"
    inner join "EnumerationValue" segmentTable on segmentTable."pkEnumerationValueId"="Reservation"."fkSegmentId"
    inner join "EnumerationValue" rateTypeTable on rateTypeTable."pkEnumerationValueId"="Reservation"."fkRateTypeId"
    inner join "ReservationDuration" on "ReservationDuration"."pkDurationLocationId"="Reservation"."fkDurationId"
    where "Reservation"."IsDeleted"='N' 

它回来了我:

ORA-00936: missing expression
00936. 00000 -  "missing expression"
*Cause:    
*Action:
Error at Line: 4 Column: 17

oracle pl / sql中distinct的正确格式是什么?我想返回所有不同的"pkRentalId"值以及所有其他列的最大值。

2 个答案:

答案 0 :(得分:1)

如果您想要每个其他列的最大值,则需要在所有其他列上执行GROUP BY,而不是DISTINCT聚合函数max

select "Rental"."pkRentalId",
    max(to_number("Reservation"."ReservationNo")) "ReservationNo",
    max(to_char("Rental"."RentalNo")) "RentalNo",
    max(to_char(segmentTable."Text"||'-'||rateTypeTable."Text")) "RateType",
    max(to_char("Debtor"."DebtorName")) "DebtorName",
    max(to_char("Rates"."RateName")) "RateName", 
    max(to_char("Renter"."FirstName"||' '||"Renter"."LastName")) "Renter",
    max(to_date("Rates"."ValidFrom")) "ValidFrom",
    max(to_date("Rates"."ValidTo")) "ValidTo",
    max(round(to_number(to_number("ReservationDuration"."ExpectedCheckinDateTime"-"ReservationDuration"."DispatchDateTime") *to_number("Rates"."UnitRate")),2)) "RentalAmount",
    max(to_number("Deductions"."Amount")) "Deduction",
    max(to_number("Billing"."DiscountAmount")) "Discount"
  from ...
 where ...
 group by "Rental"."pkRentalId"

现在,从需求的角度来看,根据您的查询,想要获得所有其他列的最大值似乎并不常见。这将以多种方式将来自多行的数据混合在一起,这对我来说似乎没有意义。

答案 1 :(得分:0)

Postgres的distinct on运算符通常会转换为使用窗口函数row_number(),其中“distinct”列用于partition by部分,而order by用于选择你想要的行:

select *
from (
  select 
      row_number() over partition by ("Rental"."pkRentalId" order by "Reservation"."ReservationNo") as rn, 
      ... other columns 
  from "Reservation" 
     ... joins 
  where "Reservation"."IsDeleted"='N' 
) 
where rn = 1

通过更改窗口定义中的order by,您可以选择所需的行(“first”,“last”,“third”)。

使用max()的解决方案的不同之处在于它将保留属于一行的值。