计算甲骨文第一季度与第二季度的收入增长-> 我有一个销售表,其销售额为dt_invoice_date列。 我如何在Oracle中做到这一点。我使用的年度收入增长代码是-
select yr1, yr2, sales1,sales2, (sales1-sales2)/sales2*100 as growth
from
(select extract(year from dt_invoice_date) yr1, sum(sale_value) as sales1 from base_sales_data_tbl
group by extract(year from dt_invoice_date) )S1
left outer join
(select extract(year from dt_invoice_date) yr2, sum(sale_value) as sales2 from base_sales_data_tbl
group by extract(year from dt_invoice_date)
) S2
ON yr1-1= yr2 ;
以类似的方式如何计算Oracle中的Qtr1 vs Qtr2收入增长。感谢您的提前帮助。
答案 0 :(得分:0)
一种方法是按年份和季度计算季度和组:
select qtr1, yr1, qtr2, yr2, sales1,sales2, (sales1-sales2)/sales2*100 as growth
from
(select extract(year from dt_invoice_date) yr1, TRUNC(EXTRACT(MONTH FROM dt_invoice_date) / 3) qtr1, sum(sale_value) as sales1 from base_sales_data_tbl
group by extract(year from dt_invoice_date), TRUNC(EXTRACT(MONTH FROM dt_invoice_date) / 3)
)S1
left outer join
(select extract(year from dt_invoice_date) yr2, TRUNC(EXTRACT(MONTH FROM dt_invoice_date) / 3) qtr2, sum(sale_value) as sales2 from base_sales_data_tbl
group by extract(year from dt_invoice_date), TRUNC(EXTRACT(MONTH FROM dt_invoice_date) / 3)
) S2
ON (yr1 = yr2
AND qtr1-1 = qtr2)
OR (yr1-1 = yr2
AND qtr1 = 1
AND qtr2 = 4);
答案 1 :(得分:0)
您还可以使用此:
with s1 as (select sum(s.sale_value) sales, to_char(dt_invoice_date,'yyyyq') yrq, lag(sum(s.sale_value),1) over (order by to_char(dt_invoice_date,'yyyyq')) sales_pq
from base_sales_data_tbl s
-- added to restrict dates to 1st and 2nd quarter
where to_char (dt_invoice_date,'q') < 3
group by to_char(dt_invoice_date,'yyyyq'))
select yrq, lag(yrq,1) over (order by yrq) prev_q, sales, sales_pq, round((sales-sales_pq)*100/sales_pq,2) growth
from s1
order by yrq;