简体   繁体   English

postgreSQL - 查询结果不正确

[英]postgreSQL - Incorrect query results

I have the following two queries:我有以下两个查询:

Return the number of flights for each manufacturer:

SELECT COUNT(flight) AS num_flights, manufacturer
FROM flights, planes
WHERE flights.tailnum = planes.tailnum
GROUP BY manufacturer
ORDER BY num_flights DESC

That returns (not all results visible here, 27 rows in total):返回(此处并非所有结果都可见,总共 27 行):

-------------+---------------------------------+--+--+--+
| num_flights | manufacturer                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 67623       | "BOEING"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 36244       | "AIRBUS INDUSTRIE"              |  |  |  |
+-------------+---------------------------------+--+--+--+
| 11676       | "AIRBUS"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 8932        | "MCDONNELL DOUGLAS AIRCRAFT CO" |  |  |  |
+-------------+---------------------------------+--+--+--+
| 4856        | "EMBRAER"                       |  |  |  |
+-------------+---------------------------------+--+--+--+
| 3998        | "MCDONNELL DOUGLAS"             |  |  |  |
+-------------+---------------------------------+--+--+--+
| 1259        | "MCDONNELL DOUGLAS CORPORATION" |  |  |  |
+-------------+---------------------------------+--+--+--+
| 247         | "CESSNA"                        |  |  |  |
+-------------+---------------------------------+--+--+--+
| 162         | "PIPER"                         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 65          | "BELL"                          |  |  |  |
+-------------+---------------------------------+--+--+--+
| 63          | "DEHAVILLAND"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 63          | "FRIEDEMANN JON"                |  |  |  |
+-------------+---------------------------------+--+--+--+
| 55          | "STEWART MACO"                  |  |  |  |
+-------------+---------------------------------+--+--+--+
| 54          | "LAMBERT RICHARD"               |  |  |  |
+-------------+---------------------------------+--+--+--+
| 51          | "KILDALL GARY"                  |  |  |  |
+-------------+---------------------------------+--+--+--+
| 47          | "BEECH"                         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 44          | "MARZ BARRY"                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 42          | "AMERICAN AIRCRAFT INC"         |  |  |  |
+-------------+---------------------------------+--+--+--+
| 40          | "LEBLANC GLENN T"               |  |  |  |
+-------------+---------------------------------+--+--+--+
| 32          | "AGUSTA SPA"                    |  |  |  |
+-------------+---------------------------------+--+--+--+
| 27          | "SIKORSKY"                      |  |  |  |
+-------------+---------------------------------+--+--+--+
| 25          | "PAIR MIKE E"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 22          | "DOUGLAS"                       |  |  |  |
+-------------+---------------------------------+--+--+--+
| 19          | "LEARJET INC"                   |  |  |  |
+-------------+---------------------------------+--+--+--+
| 18          | "AVIAT AIRCRAFT INC"            |  |  |  |
+-------------+---------------------------------+--+--+--+
| 17          | "HURLEY JAMES LARRY"            |  |  |  |
+-------------+---------------------------------+--+--+--+
| 13          | "GULFSTREAM AEROSPACE"          |  |  |  |
+-------------+---------------------------------+--+--+--+

And another one:还有一个:

Return manufacturers with more than 200 planes:

SELECT COUNT(tailnum) AS num_planes, manufacturer 
FROM planes 
GROUP BY manufacturer 
HAVING COUNT(*) > 200 
ORDER BY num_planes DESC

That returns:返回:

+------------+--------------------+
| num_planes | manufacturer       |
+------------+--------------------+
| 1630       | "BOEING"           |
+------------+--------------------+
| 400        | "AIRBUS INDUSTRIE" |
+------------+--------------------+
| 368        | "BOMBARDIER INC"   |
+------------+--------------------+
| 336        | "AIRBUS"           |
+------------+--------------------+
| 299        | "EMBRAER"          |
+------------+--------------------+

Now I am trying to query the number of flights for each manufacturer that has more than 200 planes.现在我正在尝试查询拥有超过 200 架飞机的每个制造商的航班数量。

Wrote the following query:编写了以下查询:

SELECT COUNT(flight) AS num_flights, pl.manufacturer
FROM flights fl, planes pl JOIN
(SELECT COUNT(tailnum) AS num_planes, pl2.manufacturer
    FROM planes pl2
    GROUP BY pl2.manufacturer 
    HAVING COUNT(*) > 200
    ORDER BY num_planes DESC) tm
    ON pl.manufacturer = tm.manufacturer
GROUP BY pl.manufacturer
ORDER BY num_flights DESC

However this query returns incorrect number of flights, and takes ages to execute:但是,此查询返回的航班数量不正确,并且需要很长时间才能执行:

+-------------+--------------------+
| num_flights | manufacturer       |
+-------------+--------------------+
| 262029020   | "BOEING"           |
+-------------+--------------------+
| 64301600    | "AIRBUS INDUSTRIE" |
+-------------+--------------------+
| 59157472    | "BOMBARDIER INC"   |
+-------------+--------------------+
| 54013344    | "AIRBUS"           |
+-------------+--------------------+
| 48065446    | "EMBRAER"          |
+-------------+--------------------+

What am I doing wrong here?我在这里做错了什么?

Table structures:表结构:

planes:

CREATE TABLE planes
(
     tailnum VARCHAR(6),
     manufacturer VARCHAR(50)
)

+----------+--------------------+
| tailnum  | manufacturer       |
+----------+--------------------+
| "N10156" | "EMBRAER"          |
+----------+--------------------+
| "N102UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N103US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N104UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N10575" | "EMBRAER"          |
+----------+--------------------+
| "N105UW" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| "N107US" | "AIRBUS INDUSTRIE" |
+----------+--------------------+
| ...      | ...                |
+----------+--------------------+

flights:

CREATE TABLE flights
(
     flight INT,
     tailnum VARCHAR(6)
)

+--------+----------+
| flight | tailnum  |
+--------+----------+
| 1545   | "N14228" |
+--------+----------+
| 1714   | "N24211" |
+--------+----------+
| 1141   | "N619AA" |
+--------+----------+
| 461    | "N668DN" |
+--------+----------+
| 1696   | "N39463" |
+--------+----------+
| ...    | ...      |
+--------+----------+

You can try this (joining planes and flights by tailnum), counting tailnums and flights grouping by manufacturer and filtering by having clause on COUNT(tailnum) .您可以试试这个(通过 tailnum 加入飞机和航班),计算 tailnums 和按制造商分组的航班,并通过在COUNT(tailnum)上的子句进行过滤。

    SELECT manufacturer AS "Manufacturer", 
           COUNT(DISTINCT tailnum) AS "Number of planes",
           COUNT(flights) as "Number of flights",
      FROM planes
INNER JOIN flights
        ON (planes.tailnum=flights.tailnum)
  GROUP BY manufacturer
    HAVING COUNT(DISTINCT tailnum)>200
  ORDER BY 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM