2014-09-06 46 views
0

我正在使用Oracle 10g PLSQL, 我的查询是:ORA-00936:缺少表达不同甲骨文

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?我想返回所有不同的"pkRentalId"值和所有其他列的最大值。

+0

如果你想使用'DISTINCT',你需要在选择列表中的每一列做一个'DISTINCT'。我不确定'ON(“Rental”。“pkRentalId”)子句要做什么,但它不是有效的语法。 – 2014-09-06 06:46:44

+0

我只想在我选择的某一列上执行distinct子句。在这种情况下我必须做什么?谷歌搜索推荐我上面的语法。 正如您所见http://www.postgresql.org/docs/9.0/static/sql-select.html#SQL-DISTINCT – 2014-09-06 06:50:48

+0

这是PostgreSQL文档的链接,而不是Oracle。如果结果中有多行具有相同的'pkRentalId',你怎么知道你想保留哪些其他值? – 2014-09-06 06:53:05

回答

1

如果您希望每个其他列的最大值,则需要在所有其他列上执行GROUP BY,而不是DISTINCTmax集合函数。

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" 

现在,从需求角度来看,在给定查询的情况下,想要获得所有其他列的最大值似乎是不寻常的。这将混合多行数据,这种方式对我来说似乎没有意义。

+0

ORA-00979:不是GROUP BY表达式 上述查询将此返回给我 – 2014-09-06 07:19:56

+0

@KhurramZulfiqarAli - 您确定您在查询中的每个列上都添加了聚合函数吗? – 2014-09-06 07:20:57

+0

错过了一列..回答标记! – 2014-09-06 07:25:19

0

从Postgres的的distinct on运营商通常转化为使用这里的“不同”列用于partition by部分的窗函数row_number()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可以选择您想要的行(“第一个”,“最后一个”,“第三个”)。

与使用max()的解决方案的区别在于,它将保留属于一行的值。