2017-09-26 98 views
0

数据库 - 我工作中的Postgres 9.6.5Postgres的where子句在两列

我分析从有关航机抵港及离港美国机场管理局(RITA)的数据。 此链接(http://stat-computing.org/dataexpo/2009/the-data.html)列出了表格中的所有列。

表有以下29列

No Name Description

1 Year 1987-2008

2 Month 1-12

3 DayofMonth 1-31

4 DayOfWeek 1 (Monday) - 7 (Sunday)

5 DepTime actual departure time (local, hhmm)

6 CRSDepTime scheduled departure time (local, hhmm)

7 ArrTime actual arrival time (local, hhmm)

8 CRSArrTime scheduled arrival time (local, hhmm)

9 UniqueCarrier unique carrier code

10 FlightNum flight number

11 TailNum plane tail number

12 ActualElapsedTime in minutes

13 CRSElapsedTime in minutes

14 AirTime in minutes

15 ArrDelay arrival delay, in minutes

16 DepDelay departure delay, in minutes

17 Origin origin IATA airport code

18 Dest destination IATA airport code

19 Distance in miles

20 TaxiIn taxi in time, in minutes

21 TaxiOut taxi out time in minutes

22 Cancelled was the flight cancelled?

23 CancellationCode reason for cancellation (A = carrier, B = weather, C = NAS, D = security)

24 Diverted 1 = yes, 0 = no

25 CarrierDelay in minutes

26 WeatherDelay in minutes

27 NASDelay in minutes

28 SecurityDelay in minutes

29 LateAircraftDelay in minutes

大约有一百万行每年。

我试图找出一个计数最繁忙的机场时,延迟超过15分钟。 列DepDelay - 有延迟时间。 起源 - 是机场的原始代码。

所有数据已经​​加载到一个名为“工作时间”

我形成查询作为分阶段如下表。

  1. 部分机场,其中延迟超过15分钟

    选择产地,年份,从准时 COUNT(*)作为depdelay_count其中 depdelay> 15
    组由年份,产地 为了通过depdelay_count说明 )

  2. 现在我想拔出每年仅前10个机场 - 这我做如下

    选择x.origin,从(具有作为子查询( 选择产地,年份,COUNT(*)作为从准时 depdelay_count x.year其中 depdelay> 15 组由年,原点 为了通过depdelay_count降序 ) 选择起源,年份,排名()在(由depdelay_count desc按年份排序)作为来自子查询的排名)x其中x.rank < = 10;

  3. 现在我有了depdelay排名前10位的机场 - 我希望计算出这些机场的总航班数量。

    选择原点,从准时计数()其中原点在 (从选择x.origin(具有作为子查询( 选择产地,年份,从准时 其中 depdelay> 15 组计数()作为depdelay_count按年份,来源 order by depdelay_count desc ) select origin,year,rank()over(按年份顺序按depdelay_count desc划分)作为来自子查询的等级)x其中x。等级< = 2) 组别来源 按原产地排列;

如果我在今年子句中加入年修改第3步查询

----将是(1987年至2008年)

select origin,count(*) from ontime where year = (<YEAR>) origin in 
(select x.origin from (with subquery as (
    select origin,year,count(*) as depdelay_count from ontime 
    where 
    depdelay > 15 
    group by year,origin 
    order by depdelay_count desc 
    ) 
    select origin,year,rank() over (partition by year order by depdelay_count desc) as rank from subquery) x where x.rank <= 2) 
    group by origin 
    order by origin; 

但是我有什么价值从1987年到2008年的所有年份都要手动完成,这是我想避免的。

请帮助完善查询,以便我可以选择所有年份的数据,而无需手动选择每年。

回答

1

我发现CTE在查询中间让人困惑。你基本上可以用一个CTE /子查询做到这一点:

with oy as (
     select origin, year, count(*) as numflights, 
      sum((depdelay > 15)::int) as depdelay_count, 
      row_number() over (partition by year order by sum((depdelay > 15)::int) desc) as seqnum 
     from ontime 
     group by origin, year 
    ) 
select oy.* 
from oy 
where seqnum <= 10; 

注意使用条件聚集和利用窗口函数与聚集功能。

+0

嗨戈登 - 感谢您的回复,我同意中间的CTE令人困惑。我正在分阶段构建查询,所以我和CTE一起工作。 您的查询语法错误为 flightdata_info-> where seqnum <= 10; 错误:在语法错误或接近 “总和” LINE 3:SUM((depdelay> 15):: INT)作为depdelay_count, ^ flightdata_info => 在此期间,我期待在查询了解它的写法和工作方式错误指向 –

+0

'numflights'后面缺少一个逗号。 –

+0

@AndyNichols。 。 。谢谢。 –