如何显示每天和每年postgresql的总和

时间:2018-03-20 18:18:38

标签: postgresql

我想获得每天和每年的行数值,并显示在同一行。

第一个和第二个查询从中获取结果的数据库包括这样的表(ltg_data):

time                      lon         lat                geom
2018-01-30 11:20:21       -105.4333     32.3444          01010....

然后是我加入的一些几何形状。

一个查询:

  

SELECT to_char(time,'MM / DD / YYYY')as day,count(*)as strikes FROM counties JOIN ltg_data on ST_contains(counties.the_geom,ltg_data.ltg_geom)WHERE cwa ='MFR'and time> =(now()在时区'utc') - 间隔'50500小时'组乘以1;

结果如下:

day              strikes
01/28/2018       22
03/23/2018       15
12/19/2017       20
12/20/2017       12

第二次查询:

  

选择to_char(时间,'YYYY')作为年份,计数(*)作为来自各县的罢工JO_ ltg_data对ST_contains(counties.the_geom,ltg_data.ltg_geom)WHERE cwa ='MFR'和时间> =(现在( )在时区'utc') - 间隔'50500小时'组乘以1;

结果如下:

year            strikes
2017            32
2018            37

我想要的是:

day             daily_strikes       year          yearly_strikes
01/28/2018      22                  2018          37
03/23/2018      15                  2018          37
12/19/2017      20                  2017          32
12/20/2017      12                  2017          32

我发现所有联盟都显示了最底部的年份总数,但我希望水平地得到结果,即使有重复的年度总数。谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

您可以尝试这种方法。它不是最优的,但是至少有效:

我有一个这样的测试表:

    postgres=# select * from test;
     d      | v
------------+---
 2001-02-16 | a
 2002-02-16 | a
 2002-02-17 | a
 2002-02-17 | a
(4 wiersze)

并查询:

select
    q.year,
    sum(q.countPerDay) over (partition by extract(year from q.day)), 
    q.day, 
    q.countPerDay 
from (
    select extract('year' from d) as year, date_trunc('day', d) as day, count(*) as countPerDay from test group by day, year
) as q

所以结果如下:

    2001 |   1 | 2001-02-16 00:00:001 |           1
    2002 |   3 | 2002-02-16 00:00:001 |           1
    2002 |   3 | 2002-02-17 00:00:001 |           2

答案 1 :(得分:0)

create table strikes (game_date date,
                      strikes int
                     ) ;


insert into strikes (game_date, strikes)
values ('01/28/2018', 22),
 ('03/23/2018', 15),
 ('12/19/2017', 20),
 ('12/20/2017', 12)
 ;


select * from strikes ;


select game_date, strikes, sum(strikes) over(partition by extract(year from game_date) ) as sum_stikes_by_year
from strikes ;

"2017-12-19"    20  "32"
"2017-12-20"    12  "32"
"2018-01-28"    22  "37"
"2018-03-23"    15  "37"

这种聚合应用被称为"窗口"功能或分析功能: PostgreSQL Docs

---- EDIT --- based on comments...

create table strikes_tally (strike_time timestamp,
                          lat varchar(10),
                          long varchar(10),
                          geom varchar(10)
                     ) ;


insert into strikes_tally (strike_time, lat, long, geom)
values ('2018-01-01 12:43:00', '100.1', '50.8', '1234'),
       ('2018-01-01 12:44:00', '100.1', '50.8', '1234'),
       ('2018-01-01 12:45:00', '100.1', '50.8', '1234'),
       ('2018-01-02 20:01:00', '100.1', '50.8', '1234'),
       ('2018-01-02 20:02:00', '100.1', '50.8', '1234'),
       ('2018-01-02 22:03:00', '100.1', '50.8', '1234') ;

select to_char(strike_time, 'dd/mm/yyyy') as strike_date,
        count(strike_time) over(partition by to_char(strike_time, 'dd/mm/yyyy')) as daily_strikes,
        to_char(strike_time, 'yyyy') as year,
        count(strike_time) over(partition by to_char(strike_time, 'yyyy') ) as yearly_strikes
from strikes_tally 
 ;