2015-06-01 77 views
3

我想知道如果我可以得到一些幫助這個SQL查詢。這來自我失敗並且現在想要學習的測試。我花了很多時間在上面,看了關於SQL的教程,但我仍然沒有得到它如何寫..我該如何編寫這個SQL查詢?

有2個表(航班,occupation_flights)與航班信息和任務是:編寫一個查詢,提供未來7天的航班列表,並顯示航班,可用座位數量,佔用座位數量和佔座位百分比。

FLIGHTS:      
FLIGHT ID_COMPANY DATE  ORIGIN DESTINATION PLACES 
1003  D3  20/01/2006 MADRID LONDRES  40 
1007  AF  20/01/2006 PARIS MALAGA  12 
1003  15  30/01/2006 MADRID LONDRES  20 
1007  AF  30/01/2006 PARIS MALAGA  17 

(地演出的每個航班提供的名額)

OCCUPATION_FLIGHTS: 
FLIGHT ID_COMPANY DATE PASSENGER SEAT 
1003 IB  20/01/2006 07345128H 01V 
1003 IB  20/01/2006 1013456213 01P 
1003 IB  20/01/2006 08124356C 02V 
1003 IB  20/01/2006 57176355K 02P 
1007 AF  20/01/2006 27365138A 
1003 IB  30/01/2006 734512811 01V 
1003 IB  30/01/2006 1013456213 02V 
1003 IB  30/01/2006 57176355K 

(當座椅是空的,這意味着一個人還未出票)。 TEST DATA 我想得到一些可用的座位,我應該總結一下當每個航班和日期的乘客不是空的情況下,並從地方提取這些情況。 於是我開始與像水木清華:

SELECT 
f.flight, 
f.places - (SELECT (CASE WHEN of.passanger IS NOT NULL THEN 1 ELSE 0 END)  
FROM occupation_flights AS of GROUP BY of.flight, of.date) AS available, 
f.places - (SELECT (CASE WHEN of.passanger IS NULL THEN 1 ELSE 0 END) FROM occupation_flights AS of GROUP BY of.flight, of.date) AS occupied, 
100.0 * ((f.places - (SELECT (CASE WHEN of.passanger IS NOT NULL THEN 1 ELSE 0 END) FROM occupation_flights AS of GROUP BY of.flight, of.date))/f.places AS %occupied 
FROM flights AS f 
JOIN occupation_flights AS of ON f.flight=of.flight 
WHERE f.date BETWEEN DateAdd(DD,-7,GETDATE()) and GETDATE() 
GROUP BY f.flight 

,但是這還不算完,因爲我真的不知道如何讓他計算出它的飛行和日期,同時羣體,因爲有與航班相同的航班號,但在不同的日期。我也無法訪問表格,測試是在紙上,並顯示你知道如何編寫SQL查詢。 會真的感謝任何見解! 非常感謝提前!

+0

我將你的數據分組在sqlfiddle中:http://sqlfiddle.com/#!9/d62a4隨意編輯你的帖子,如測試數據,然後鏈接它的網址,這樣其他人也有這個數據,我也我會盡力去得到你需要的東西,但我並不是最擅長的這類事情:p –

+0

你確定你使用的是mysql嗎? getdate和dateadd不是mysql函數 –

+0

嗨,傑里米,非常感謝您的回答。我現在將查看所有評論並試圖理解這些邏輯。非常感謝這個sql小提琴,我不知道這樣的事情存在,它使事情變得更容易!我不知道是否實際上使用mysql,我在工作中總是使用pgadmin postgre或者像以前那樣,並且這個任務僅僅來自我對失敗的sql知識的測試。 – Yana

回答

0
select t1.flight, 
    t1.date, 
    t1.places, 
    t3.c, 
    t1.places-IFNULL(t3.c, 0) as empty 
from flights t1 
left join(
    select count(t2.seat) as c, t2.flight, t2.date 
     from occupation_flights t2 
    where seat is not null 
     group by flight, date 
     ) t3 
    on t1.flight= t3.flight and t1.date= t3.date 

,這裏是導致

enter image description here

PS。在我的代碼中,使用了一個MySql函數IFNULL(),應該用另一個數據庫實現中的其他類似函數替換。

+0

...像COALESCE(),它也適用於MySQL並符合ANSI標準。 – Strawberry

+0

非常感謝! – Yana

1

Mysql的符合: SQLFIDDLE

SELECT f.flight,f.mydate,f.places - count(of.seat) as available_seats, 
     count(of.seat) as occupied, 
     count(of.seat)/f.places * 100 as percentage_occupied 
FROM flights f 
JOIN occupation_flights of 
ON f.flight = of.flight AND f.mydate = of.mydate 
WHERE of.mydate BETWEEN DATE_ADD(CURDATE(), INTERVAL -7 DAY)AND CURDATE() 
group by f.flight, f.mydate 
0
SELECT 
    f.flight, f.mydate, f.id_company, origin, destination, 
    places, (places - COALESCE(t.ocupped_seats, 0)) as available_places, 
    IFNULL(t.ocupped_seats, 0) as ocupped_places, 
    CONCAT(ROUND(COALESCE(places/COALESCE(t.ocupped_seats, 0), 0), 2), '%') as percentage 
FROM 
    flights as f 
LEFT JOIN 
    (SELECT 
     flight, mydate,id_company, COUNT(seat) as ocupped_seats 
    FROM 
     occupation_flights 
    WHERE 
     seat IS NOT NULL 
    GROUP BY 
     flight, mydate) as t 
    ON 
     f.flight = t.flight AND f.mydate LIKE t.mydate 
WHERE 
    f.mydate BETWEEN CURDATE() AND CURDATE()+7 

DEMO(感謝Jeremy C.建設模式和StrawberryCOALLESCE功能)

結果:

enter image description here

+0

非常感謝! – Yana