mysql - How can I write this SQL query? -
i wondering if on sql query. it´s test failed , wanted learn. i've spent lot of time on it, watched tutorials on sql still don't how written..
there 2 tables (flights, occupation_flights) flight information , task to: write query gives list of flights next 7 days, , shows flight, number of available seats, number of occupied seats , % of occupied seats.
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
(places show number of places offered on each flight)
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
(when seat empty, means person has not yet issued ticket). test data thought number of available seats should sum cases when passenger not empty per flight , date , extract places. started smth like:
select f.flight, f.places - (select (case when of.passanger not null 1 else 0 end) occupation_flights of group of.flight, of.date) available, f.places - (select (case when of.passanger null 1 else 0 end) occupation_flights of group of.flight, of.date) occupied, 100.0 * ((f.places - (select (case when of.passanger not null 1 else 0 end) occupation_flights of group of.flight, of.date)) / f.places %occupied flights f join occupation_flights of on f.flight=of.flight f.date between dateadd(dd,-7,getdate() ) , getdate() group f.flight
but it's not finished, because don't understand how make him calculate in groups flight , date @ same time, because there flights same flight number going on different dates. don't have access tables, test on paper , show know how write sql queries. grateful insights! many in advance!
mysql conform: sqlfiddle
select f.flight,f.mydate,f.places - count(of.seat) available_seats, count(of.seat) occupied, count(of.seat)/f.places * 100 percentage_occupied flights f join occupation_flights of on f.flight = of.flight , f.mydate = of.mydate of.mydate between date_add(curdate(), interval -7 day)and curdate() group f.flight, f.mydate
Comments
Post a Comment