Postgresql查询使用多个表来获取每年的每月计数

时间:2016-12-12 13:04:44

标签: sql database postgresql postgresql-9.5

根据reference link我习惯于根据一年内的单月表计数,这是有效的。但在这里,我需要根据用户类型和遗产来获得两个具有关系的表格。

示例数据

居民表

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 |

用户表

id | name   | user_login_type | created_at          |
80 | rest   |   3             | 2016-12-01 15:11:08 |
81 | appu   |   2             | 2016-12-02 12:51:08 |
84 | sankar |   2             | 2016-12-06 11:11:29 |
85 | test   |   1             | 2016-12-06 15:06:57 |

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

示例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:如果我在join语句之后使用where where condition得到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

1 个答案:

答案 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