2016-12-12 30 views
0

根据reference link我习惯于在一年内使用单个表进行计数。但在这里,我需要根据用户类型和财产关系计算两张关系表。Postgresql查询获得一年内每月有多个表的计数

的样本数据

居民表

resident_id | resident_user_id | resident_estate_id | created_at   | 
31   | 75    |  1   | 2016-12-07 11:22:23 | 
32   | 76    |  16   | 2016-12-07 11:22:23 | 
37   | 81    |  16   | 2016-12-07 11:22:23 | 
38   | 84    |  17   | 2016-12-07 11:22:23 | 

用户表

​​

在这里我们需要根据条件和一年内列出的数据。以下示例查询用于获取数据。

示例1:如果将用户登录类型3更改为2,则意味着12月份的计数为1,但需要计入零计数。

SELECT to_char(i, 'YYYY') as year_data, to_char(i, 'MM') as month_data, to_char(i, 'Month') as month_string, count(resident_id) as ios_total_count 
FROM generate_series(now() - INTERVAL '1 year', now(), '1 month') as i 
left join residents on (to_char(i, 'YYYY') = to_char(created_at, 'YYYY') and to_char(i, 'MM') = to_char(created_at, 'MM') and resident_estate_id = 17) 
left join users on (users.id=residents.resident_user_id and users.user_login_type = 3) 
GROUP BY 1,2,3 order by year_data desc, month_data desc 
limit 12 

输出

"2016";"12";"December ";1 
"2016";"11";"November ";0 
"2016";"10";"October ";0 
"2016";"09";"September";0 
"2016";"08";"August ";0 
"2016";"07";"July  ";0 
"2016";"06";"June  ";0 
"2016";"05";"May  ";0 
"2016";"04";"April ";0 
"2016";"03";"March ";0 
"2016";"02";"February ";0 
"2016";"01";"January ";0 

例2:如果我用在获得12月当月用计数也加入语句后的状态。但在年内没有获得其他月份。

SELECT to_char(i, 'YYYY') as year_data, to_char(i, 'MM') as month_data, to_char(i, 'Month') as month_string, count(resident_id) as ios_total_count 
FROM generate_series(now() - INTERVAL '1 year', now(), '1 month') as i 
left join residents on (to_char(i, 'YYYY') = to_char(created_at, 'YYYY') and to_char(i, 'MM') = to_char(created_at, 'MM')) 
left join users on (users.id=residents.resident_user_id) 
where residents.resident_estate_id = 17 and users.user_login_type = 3 
GROUP BY 1,2,3 order by year_data desc, month_data desc 
limit 12 

期望输出

"2016";"12";"December ";0 
"2016";"11";"November ";0 
"2016";"10";"October ";0 
"2016";"09";"September";0 
"2016";"08";"August ";0 
"2016";"07";"July  ";0 
"2016";"06";"June  ";0 
"2016";"05";"May  ";0 
"2016";"04";"April ";0 
"2016";"03";"March ";0 
"2016";"02";"February ";0 
"2016";"01";"January ";0 

回答

0

您筛选其他行与where residents.resident_estate_id = 17 and users.user_login_type = 3 我假设你想,而不是水木清华这样的:

SELECT 
    DISTINCT 
    to_char(i, 'YYYY') as year_data 
    , to_char(i, 'MM') as month_data 
    , to_char(i, 'Month') as month_string 
    , count(resident_id) 
    filter (where residents.resident_estate_id = 17 and users.user_login_type = 3) 
    over (partition by date_trunc('month',i)) as ios_total_count 
FROM generate_series(now() - INTERVAL '1 year', now(), '1 month') as i 
left join residents on (to_char(i, 'YYYY') = to_char(created_at, 'YYYY') and to_char(i, 'MM') = to_char(created_at, 'MM')) 
left join users on (users.id=residents.resident_user_id) 
where residents.resident_estate_id = 17 and users.user_login_type = 3 
order by year_data desc, month_data desc 
limit 12 
+0

@维罗,获取12月当月只有结果。但我需要在这里剩下的几个月的计数为零 –

+0

更新问题与一些示例数据?.. –

+0

示例数据更新与问题。等待你的回答这个 –

相关问题