2013-05-29 73 views
0

想要提前選擇旅遊日期和網站名稱的總費用超過230,並且該旅遊已超過7人。完整的代碼如下,工會的第一部分工作。sql中存在無效標識符錯誤的子查詢

SELECT tour_date AS "Departure Date", site_name "Site Name" 
FROM partres, reservation, tour, site 
WHERE partres.res_id = reservation.res_id 
    AND reservation.tour_id = tour.tour_id 
    AND tour.site_id = site.site_id 
GROUP BY tour_date, site_name 
HAVING COUNT(part_id) > 7 

    UNION 

SELECT tour_date AS "Departure Date", site_name "Site Name" 
FROM (
    SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost" 
    FROM reservation,site,tour) 
WHERE reservation.tour_id = tour.tour_id 
    AND tour.site_id = site.site_id 
    AND total_cost > 230 
GROUP BY tour_date, site_name; 

我仍然有錯誤,

ORA-00904: "TOTAL_COST": invalid identifier 
00904. 00000 - "%s: invalid identifier" 
*Cause:  
*Action: 
Error at Line: 437 Column: 7 

感謝

+0

認爲你忘了把total_cost放到第一個SELECT語句 – krasu

+1

你想完成什麼?內部查詢沒有where子句,因此您沒有按照任何標準加入表。在您的內部查詢之外,字段預留。*,旅遊。*和網站。*不可用。 – jvilhena

+0

想要提前選擇旅遊日期和地點名稱的總費用超過230,並且該旅遊已超過7人。 –

回答

2

你需要移動的子查詢中的連接條件

SELECT tour_date AS "Departure Date", site_name "Site Name" 
FROM (
    SELECT res_id,tour_date,site_name, (res_partcost +NVL(RES_GEARCOST,0)) as "total_cost" 
    FROM reservation,site,tour 
    WHERE reservation.tour_id = tour.tour_id 
    AND tour.site_id = site.site_id) Res1 
WHERE Res1.total_cost > 230 // this will not be displayed in a result 
GROUP BY tour_date, site_name; 
0

一個union是不會給你一個「和」之間的條件。它會給你一個「或」(因爲任何符合任何條件的東西都會被包含在內)。

我認爲你應該用一個查詢來描述這個子查詢。此外,適當的加入語法是一個好處:

SELECT tour_date AS "Departure Date", site_name "Site Name" 
FROM partres p join 
    reservation r 
    on p.res_id = r.res_id join 
    tour t 
    on r.tour_id = t.tour_id and 
     (res_partcost + coalesce(RES_GEARCOST,0)) > 230 join 
    site s 
    on t.site_id = s.site_id 
GROUP BY tour_date, site_name 
having COUNT(part_id) > 7; 

這個版本正在猜測,total_costreservation表。

+0

謝謝,但我需要使用聯盟 –

+0

我完成了工會,非常感謝 –

相關問題