难以构造事件的SQL查询

时间:2017-07-03 04:28:13

标签: mysql sql

我遇到了一个问题,我试图获取与不同日期发生的多个事件相关的数据计数。

假设我正在追踪一群飞机观察员,他们每个人都可以在某个特定的机场找到来自世界各地的许多飞机。我想制作一个每天每个观察者一行的清单,其中包括观察者ID,日期,当天发现的飞机数量(它总是“他”,对吗?),有多少个别航空公司飞机属于这些飞机,以及航空公司所属的国家数量。所以,我想得到这样的结果:

 +-----------+------------+---------+----------+-----------+
 |           |            |  Planes |          |   Airline |
 | SpotterID | Date       | spotted | Airlines | Countries |
 +-----------+------------+---------+----------+-----------+
 |      1234 | 2017-04-15 |      28 |       11 |         4 |
 +-----------+------------+---------+----------+-----------+
 |      1234 | 2017-04-16 |      65 |       19 |         7 |
 +-----------+------------+---------+----------+-----------+
 |      5678 | 2017-04-22 |      39 |       14 |         6 |
 +-----------+------------+---------+----------+-----------+
 |      6677 | 2017-04-28 |      74 |       29 |         9 |
 +-----------+------------+---------+----------+-----------+

所以,(MySQL 5.7.17)我的测试表如下图所示(我可能忘记了几个索引)。

飞机定位事件表定义为:

CREATE TABLE `SpottingEvents` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `SpotterID` int(11) NOT NULL,
 `SpotDateTime` datetime NOT NULL,
 `PlaneID` int(11) NOT NULL,
 `Notes` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `indx_SpotterID_PlaneID_DateTime` (`SpotterID`,`PlaneID`,`SpotDateTime`),
 KEY `indx_SpotterID_DateTime` (`SpotterID`,`SpotDateTime`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8

飞机表定义为:

CREATE TABLE `Planes` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `PlaneID` int(11) NOT NULL,
 `PlaneTypeID` int(11) NOT NULL,
 `AirlineID` int(11) NOT NULL,
 `Notes` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `indx_PlaneID_AirlineID` (`PlaneID`,`AirlineID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8

航空公司表定义为:

CREATE TABLE `Airlines` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `AirlineID` int(11) NOT NULL,
 `AirlineName` varchar(100) NOT NULL,
 `CountryID` int(11) NOT NULL,
 `Notes` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `indx_AirlineID` (`AirlineID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8

和countries表定义为:

CREATE TABLE `Countries` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `CountryID` int(11) NOT NULL,
 `CountryName` varchar(100) NOT NULL,
 `Notes` varchar(100) NOT NULL,
 PRIMARY KEY (`id`),
 UNIQUE KEY `indx_CountryID` (`CountryID`),
) ENGINE=InnoDB DEFAULT CHARSET=utf8

我的问题在于最后两列,航空公司和国家/地区。我尝试了几种方法,包括以下内容:

select distinct sev.SpotDateTime, sev.SpotterID, count(*) as planes_count,
(select count(*) from ( select distinct cnt.CountryID 
  from Countries as cnt
  inner join Airlines as aln on al.CountryID = cnt.CountryID
  inner join Planes as pl on pl.AirlineID = aln.AirlineID 
  where pl.PlaneID = sev.PlaneID ) as t1) as countries_count  
from SpottingEvents as sev
# where sev.UserID = 1234
group by SpotDateTime
order by SpotDateTime

导致错误Unknown column 'sev.planeID' in 'where clause' 但由于我不是专家,我只是没有得到预期的结果。那么,我如何达到预期的效果呢?

1 个答案:

答案 0 :(得分:1)

您正在寻找COUNT(DISTINCT)。加入所需的表格,然后计算。

select
  se.spotterid,
  date(se.spotdatetime) as spot_date,
  count(*) as planes,
  count(distinct p.airlineid) as airlines,
  count(distinct a.countryid) as countries
from spottingevents se
join planes p on p.planeid = se.planeid
join airlines a on a.airlineid = p.airlineid
group by se.spotterid, date(se.spotdatetime)
order by date(se.spotdatetime), se.spotterid;