创建上个月和前12个月的摘要表,然后添加百分比差异

时间:2019-03-18 09:37:55

标签: sql oracle sum percentage datediff

我正在尝试创建一个表,该表提供公司在县内每个地区收到的所有不同查询类型的摘要。

理想情况下,我想要的是以下内容:

面积||主题类型||上个月的Enq ||前12个月的平均enq ||差异百分比

到目前为止,我编写的代码在下面,但我认为这根本不对(我对Oracle SQL相当陌生)。

在此方面的任何帮助将不胜感激。

Select 
central_enquiry.enquiry_number,
central_enquiry.enquiry_time,
enquiry_subject.subject_name,
area.area_name

Sum(Case when datediff(month, date, current_timestamp) between 1 and 12 then column_value else 0. End) /12 as avg_column_value, 
Sum(Case when datediff(month, date, current_timestamp) = 0 then column_value else 0. end) as sum_current_column_value,
Sum ((100/avg_column_value) * sum_current_column_value) as Percentage_Difference

From
Central_enquiry,
central_site,
enquiry_subject,
area

Where
datediff(month, date, current_timestamp between) 0 and 12 

AND enquiry_subject.service_code = central_enquiry.service_code 
AND central_site.area_code = area.area_code 
AND central_site.site_code = central_enquiry.site_code

0 个答案:

没有答案