2015-06-27 20 views
1

我在Postgres的工作用表这样的划分:如何行之间在一个表中的Postgres

mon yyyy weather 
Apr 2014 78.45 
Apr 2015 77.32 
May 2014 79.56 
May 2015 78.43 

我希望能够查询了一些成果,通过“星期一”命令,其中weather列值按月份按年份划分。

换句话说,我要查询天气,其中四月2015年由四月分2014年

不过,我想编写查询中,使得我没有指定一个月或一年的方式,和查询自动根据2015年4月/ 2014年4月,然后2014年5月/ 2014年5月将值分为weather,而无需每月和每年都输入,这很费力。

我有下面的代码,但这种扩展的列这不是我想要的:

select (select "Weather" from yoy 
     where mon = 'Apr' and yyyy = '2015' 
    )/(select "American" from yoy 
     where mon = 'Apr' and yyyy = '2014' 
    ) as "weather_apr", 
    (select "Weather" from yoy 
     where mon = 'May' and yyyy = '2015' 
    )/(select "Weather" from yoy 
     where mon = 'May' and yyyy = '2014' 
    ) as "weather_may", 
from yoy; 

回答

1

在我看来,这是采取的分析window function好处的情况。这里没有加入的魔法:

SELECT yyyy,  
     weather, 
     mon, 
     lead(weather) over (partition by mon order by mon, yyyy desc), 
     weather/lead(weather) over (partition by mon order by mon, yyyy desc) 
FROM joy 
0

我认为你需要自连接就像下面的例子:

SELECT j1."yyyy" As year, 
     j2."yyyy" As next_year, 
     j1."mon", 
     j1."weather", 
     j2."weather" As "weather from next year", 
     j1."weather"::float/j2."weather" As divide 
FROM joy j1 
JOIN joy j2 
ON j1."yyyy" = j2."yyyy" - 1 AND j1."mon" = j2."mon" 

演示: http://sqlfiddle.com/#!15/e02ec/1

0

您可以使用表yoy的自加入。看到这个查询,它连接当前年与以前之一:

select * 
from yoy a 
join yoy b on a.yyyy::int = b.yyyy::int+ 1 and a.mon = b.mon 

mon | yyyy | weather | mon | yyyy | weather 
-----+------+---------+-----+------+--------- 
Apr | 2015 | 77.32 | Apr | 2014 | 78.45 
May | 2015 | 78.43 | May | 2014 | 79.56 
(2 rows) 

现在你可以很容易地做你的计算:

select a.mon, a.weather/ b.weather as weather 
from yoy a 
join yoy b on a.yyyy::int = b.yyyy::int+ 1 and a.mon = b.mon 

mon |  weather 
-----+------------------------ 
Apr | 0.98559592096876991714 
May | 0.98579688285570638512 
(2 rows) 
0

我觉得有条件的聚集可以为这种类型的非常有用查询:

select mon, 
     max(case when yyyy = 2014 then weather end) as weather_2014, 
     max(case when yyyy = 2015 then weather end) as weather_2015, 
     (max(case when yyyy = 2015 then weather end)/
     max(case when yyyy = 2014 then weather end) 
     ) as ratio 
from yoy 
group by mon 

这假定你想要行减少到每个星期一日。为了让先前的值,只需使用lag()

select yoy.*, 
     lag(weather) over (partition by month order by year) as prev_weather 
from yoy; 
相关问题