-1

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

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);

我必须像我一样按飞机类型列出航空公司运送的平均乘客人数吗?尝试使用 avg () 但我在加入时不使用它。

4

1 回答 1

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

一个用于测试的 SQLfiddle

于 2013-07-24T19:39:30.237 回答