join - SQL 4 tables query -


can me queries wrote, , didn't work correctly, maybe except first one.

we have 4 tables - columns:

table_cars - id | brand | type | license table_equipments - id | name | description table_distances - id_car | date | distance table_cars_equipments - id_car | id_equipment 

and questions:

  1. show cars have equipment "fire extinguisher" , have been driving yesterday
  2. show brands no equipments
  3. show total distance driven car "xxxx" in last month
  4. show average distance per day driven cars "some description"

my solution:

   select    table_cars.id,   table_cars.brand, table_equipments inner join table_cars_equipments  on table_equipments.id     =  table_car_equipments.id_equipment  inner join table_cars  on table_cars .id     = table_cars_equipments.id_car inner join table_distances on table_distances.id_car = table_cars.id table_equpments.name = 'fire extinguisher'   , table_date.date = now() - interval 1 days;  select table_cars.brand table_cars inner join table_cars_equipments  on  table_cars_equipments.id_car   =  table_cars .id  table__car_equpments.id_equipment = 0 or null  select table_cars.license, sum(distance) distance, table_distances inner join table_cars on table_distances. id_car = table_cars.id table_distances.date >= now() - interval 30 days , table.distances.date <= now()  select avg(distance) avg_dist from(  select table_distances.distance, count(*) dist table_distances inner join table_cars_equipment on table_dustances.id_car = table_cars_equipments.id_car inner join table_equipments on  table_cars_equipments.id_equipments = table_equimpents.id table_equipments.distance = "ilfov" groupby table_distances.date) a; 

1.show cars have equipment "fire extinguisher" , have been driving yesterday

select c.* table_cars c inner join table_distances d on c.id = d.id_car  inner join table_cars_equipments ce on c.id = ce.id_car inner join table_equipments e on ce.id_equipment = e.id e.description = 'fire extinguisher'   , trunc(d.date) = trunc(sysdate - 1); 

2.show brands no equipments

select distinct brand table_cars c not exists (   select 'x' table_cars_equipments ce   ce.id_car = c.id ); 

3.show total distance driven car "xxxx" in last month

select sum(distance)  total_distances d inner join table_cars c on d.id_car = c.id , c.brand = 'xxx' to_char(d.date,'mon') = to_char(trunc(sysdate,'mon')-1 , 'mon') 

4.show average distance per day driven cars "some description"

select trunc(d.date) day ,avg(distance) table_cars c inner join table_distances d on c.id= d.id_car c.brand='some car' group trunc(d.date); 

date functions here specific oracle!


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -