Oracle SQL-计算每月作业数

时间:2019-11-08 13:51:31

标签: sql oracle count

我希望统计每个地区每个月出现的工作类型(G012)的总时间。状态代码为“ 5200”。

到目前为止,我的代码是:

select area.area_name, count(area.area_name)
from area
inner join central_site on central_site.area_code = area.area_code
inner join job on job.site_code = central_site.site_code
inner join job_type on job.job_type_key = job_type.job_type_key
inner join job_status_log on job.job_number = job_status_log.job_number
where job_type.job_type_key = 'G012' and job_status_log.status_code = '5200'
group by area.area_name
order by area.area_name

我不确定如何计算这个数字,以便每个月都属于不同的类别-但我最终想得到的是这样的东西:

Area             April     May     June    July    August    September    October
North Ward         2        5        3       2        7         4            3
South Ward         2        3        4       4        6         4            4

1 个答案:

答案 0 :(得分:1)

基本上,您还必须按月份对数据进行分组:

input CreatepostsInput {
}

您也可以使用select area.area_name, EXTRACT(MONTH FROM job.actual_start_date) mon, count(area.area_name) from area inner join central_site on central_site.area_code = area.area_code inner join job on job.site_code = central_site.site_code inner join job_type_key on job.job_type_key = job_type.job_type_key inner join job_status_log on job.job_number = job_status_log.job_number where job_type.job_type_key = 'G012' and job_status_log.status_code = '5200' group by area.area_name, EXTRACT(MONTH FROM job.actual_start_date) order by area.area_name 代替数字EXTRACT(MONTH FROM job.actual_start_date)

要获得所需的输出(月以列而不是行的形式),可以使用数据透视:

TO_CHAR(job.actual_start_date, 'Month')

要同时按年份分隔数据,只需在现在有SELECT * FROM (select area.area_name, EXTRACT(MONTH FROM job.actual_start_date) mon from area inner join central_site on central_site.area_code = area.area_code inner join job on job.site_code = central_site.site_code inner join job_type_key on job.job_type_key = job_type.job_type_key inner join job_status_log on job.job_number = job_status_log.job_number where job_type.job_type_key = 'G012' and job_status_log.status_code = '5200') PIVOT ( COUNT(*) FOR mon IN (1 AS January, 2 AS February, 3 AS March, 4 AS April, 5 AS May, 6 AS June, 7 AS July, 8 AS August, 9 AS September, 10 AS October, 11 AS November, 12 AS December) ) 的任何地方添加一个EXTRACT(YEAR FROM job.actual_start_date)列即可。