不同表的平均问题

时间:2013-07-24 17:53:07

标签: sql join average

我有一个具有以下结构的数据库,其中包含以下寄存器

CREATE TABLE  aircraft  (
   number_aircraft  int(11) ,
   type_aircraft  VARCHAR(10),
  PRIMARY KEY ( number_aircraft )
);


CREATE TABLE  passenger  (
   number_flight  int(11) ,
   ID  int(11),
   Name  char(10) DEFAULT NULL,
  PRIMARY KEY ( number_flight , ID ),
  FOREIGN KEY ( number_flight ) REFERENCES  flight  ( number_flight )
);


CREATE TABLE  flight  (
   number_flight  int(11) ,
   since  char(2) ,
   until  char(2) ,
   Date_flight  date ,
   number_aircraft  int(11),
  PRIMARY KEY ( number_flight,number_aircraft ),
  FOREIGN KEY ( number_aircraft ) REFERENCES  aircraft  ( number_aircraft )
);



INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (1,'B-777');
INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (2,'B-755');
INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (3,'B-775');
INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (4,'B-505');
INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (5,'Airbus A3');
INSERT INTO  aircraft  ( number_aircraft , type_aircraft ) VALUES (6,'B-707');

INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (1,34889876,'German');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (1,44859776,'Ariel');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (2,34587880,'Andres');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (2,34886463,'David');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (3,24757385,'Juan');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (3,34885768,'Marcos');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (4,10000001,'Bárbara');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (4,20000002,'Daniela');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (5,20000003,'Alejandra');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (5,20000004,'Melina');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (6,30000001,'Daniela');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (6,30000002,'Valeria');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (7,40000001,'Silvana');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (7,50000001,'Cecilia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (8,50000002,'Noelia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (8,50000003,'Cintia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (9,60000001,'Mariela');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (9,60000002,'Ornella');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (10,60000003,'Cecilia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (10,60000004,'Valeria');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (11,60000005,'Agustina');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (11,60000006,'Aldana');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (12,60000007,'Carolina');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (12,60000008,'Cecilia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (13,70000001,'Bárbara');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (13,70000002,'Gisele');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (14,70000003,'Cecilia');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (14,70000004,'Vicuntilria');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (15,60000009,'Sabrina');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (15,60000010,'Agostina');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (16,80000001,'Andrea');
INSERT INTO  passenger  ( number_flight , ID , Name ) VALUES (16,80000002,'Tamara');

INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (1,'A','A','2001-02-14',1);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (2,'A','F','2010-05-15',1);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (3,'A','D','2012-08-09',4);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (4,'B','H','2001-02-14',3);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (5,'D','H','2005-06-17',4);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (6,'A','D','2007-03-17',1);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (7,'A','F','2001-02-19',2);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (8,'C','F','2011-03-14',2);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (9,'A','B','2013-03-02',4);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (10,'A','B','2010-03-12',3);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (11,'A','D','2011-04-28',3);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (12,'C','B','2013-01-01',4);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (13,'B','D','2013-03-21',5);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (14,'A','B','2012-03-02',5);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (15,'D','H','2013-06-06',6);
INSERT INTO  flight  ( number_flight , since , Until , Date_flight , number_aircraft ) VALUES (16,'A','H','2013-12-13',6);

我必须列出飞机运送的平均乘客数量 公司,按飞机类型,和我一样?尝试使用av​​g()但是 我加入时不会使用它。

1 个答案:

答案 0 :(得分:0)

如果我理解正确,应该这样做,使用两个COUNT DISTINCT代替AVG更简单; (对不起,原来问题的名字)

SELECT a.Tipo_avion, COUNT(DISTINCT p.Documento)/COUNT(DISTINCT v.Nro_vuelo) avg_passengers
FROM vuelo v
LEFT JOIN pasajero p
  ON v.Nro_Vuelo = p.Nro_Vuelo
LEFT JOIN avion a
  ON v.Nro_Avion = a.Nro_Avion
GROUP BY a.Tipo_avion

An SQLfiddle to test with