2017-07-03 60 views
1

我遇到了一個問題,我試圖獲取與在不同日期發生的多個事件相關的數據計數。在構建事件的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 

和國家表定義爲:

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

差不多,但請參閱https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple -sql-query – Strawberry

+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; 
+0

真棒,這是完美的,謝謝你Thorsten!我將不得不仔細研究這個問題,因爲將來我會需要這樣的事情。我不得不補充說,這不是關於飛機探測者,而是與參與者追求技術愛好中的事件有關。還要感謝Strawberry爲我指出發佈此類查詢的指導方針。 –