好你的幾個問題。
首先,在子查詢之前的查詢中有兩個FROM
,子查詢之後有一個。
其次,你沒有GROUP BY
,但你有一個聚合函數被使用。
它看起來像你可能想:
SELECT BOOKING_ID, ROOM_NO , SUM(TOTAL) AS TOTAL
FROM
(
SELECT BOOKING_ID,
ROOM_NO,
(END_DATE-START_DATE) DAYDIFF,
((END_DATE-START_DATE)*ROOM_PRICE) TOTAL
FROM ROOM_TBL
NATURAL JOIN ROOM_DETAILS_TBL
WHERE BOOKING_ID = &BOOKING_ID
)
GROUP BY BOOKING_ID, ROOM_NO;
編輯,如果你想有一個TOTAL
行,那麼你可以使用GROUP BY ROLLUP
:
SELECT max(BOOKING_ID) Booking_id,
case when ROOM_NO is null then ' ' else room_no end Room_no,
SUM(TOTAL) AS TOTAL
FROM
(
SELECT BOOKING_ID,
ROOM_NO,
(END_DATE-START_DATE) DAYDIFF,
((END_DATE-START_DATE)*ROOM_PRICE) TOTAL
FROM ROOM_TBL
NATURAL JOIN ROOM_DETAILS_TBL
WHERE BOOKING_ID = &BOOKING_ID
)
GROUP BY ROLLUP (room_no);
見SQL Fiddle with Demo
如果你想列中的數據,則可以在子查詢上使用公用表表達式:
with cte as
(
SELECT BOOKING_ID,
ROOM_NO,
(END_DATE-START_DATE) DAYDIFF,
((END_DATE-START_DATE)*ROOM_PRICE) TOTAL
FROM ROOM_TBL
NATURAL JOIN ROOM_DETAILS_TBL
)
select booking_id,
room_no,
total,
case when rn = 1 then totalbooking else null end totalbooking
from
(
select booking_id,
room_no,
total,
(select sum(total) from cte) TotalBooking,
row_number() over(partition by booking_id order by room_no) rn
from cte
)
見SQL Fiddle with Demo
太多櫨那裏,刪除後的最後一個) – 2013-03-20 17:53:45
的我喜歡Oracle的東西是它將任何錯誤轉化爲「缺少括號」錯誤。我很確定,如果磁盤空間不足,你會得到'缺少括號'。 – 2013-03-20 17:55:36