psql计算环比和同比
2017-04-17 14:23
645 查看
\c dw; –连接到数据库
drop table if exists stg.d_mars_rate_hb_1 ;
create table if not exists stg.d_mars_rate_hb_1(
category_1 TEXT,
date DATE,
quantity numeric(30,16)
);
insert into stg.d_mars_rate_hb_1(
category_1,
date,
quantity
)select
category_1,
date,
quantity
from stg.d_mars_order_date ;–从表里面选出需要的字段备用
drop table if exists stg.d_mars_cat1_month_hb;
create table if not exists stg.d_mars_cat1_month_hb(
category_1 TEXT,
hb NUMERIC(30,16)
);
insert into stg.d_mars_cat1_month_hb
//计算出每天category_1对应的值作为原表
with every as (
select category_1,date,sum(quantity) as quantity
from stg.d_mars_rate_product group by category_1,date order by date),
//计算出最近一天的category_1的对应值作为副表1
max_date as(
select category_1, max(date) as date from every where quantity is not null group by category_1),
//将副表1与原表left join 得到最近一个月的值,因为之前的原表没有quantity字段,所以left join一下得到quantity字段
quantity as(
select d.category_1,r.date,d.quantity
from max_date r
left join every d
on r.category_1 = d.category_1 and d.date > (r.date - interval ‘1 month’)::date and d.date <= r.date ),
//又将副表1与原表left join得到的表作为副表2
hb_1 as(
select a.category_1,
//最近一个月的quantity
sum(case when a.date = e.date then a.quantity end) as current_quantity,
//上一个月的quantity,如果求同比的话,将’1 month ’ 换成’12 month’
sum(case when (e.date > (a.date - interval ‘1 month’)::date and e.date <= a.date) then e.quantity end) as preview_quantity
from quantity a
left join every e
on a.category_1 = e.category_1
group by a.category_1
),
result as(
select category_1,(current_quantity - preview_quantity )/ preview_quantity as hb
from hb_1 )
select * from result ;
环比增长计算公式:(这个月的quantity-上个月的quantity)/上个月的quantity
同比增长计算公式:(这个月的quantity-去年同月的quantity)/去年同月的quantity
drop table if exists stg.d_mars_rate_hb_1 ;
create table if not exists stg.d_mars_rate_hb_1(
category_1 TEXT,
date DATE,
quantity numeric(30,16)
);
insert into stg.d_mars_rate_hb_1(
category_1,
date,
quantity
)select
category_1,
date,
quantity
from stg.d_mars_order_date ;–从表里面选出需要的字段备用
drop table if exists stg.d_mars_cat1_month_hb;
create table if not exists stg.d_mars_cat1_month_hb(
category_1 TEXT,
hb NUMERIC(30,16)
);
insert into stg.d_mars_cat1_month_hb
//计算出每天category_1对应的值作为原表
with every as (
select category_1,date,sum(quantity) as quantity
from stg.d_mars_rate_product group by category_1,date order by date),
//计算出最近一天的category_1的对应值作为副表1
max_date as(
select category_1, max(date) as date from every where quantity is not null group by category_1),
//将副表1与原表left join 得到最近一个月的值,因为之前的原表没有quantity字段,所以left join一下得到quantity字段
quantity as(
select d.category_1,r.date,d.quantity
from max_date r
left join every d
on r.category_1 = d.category_1 and d.date > (r.date - interval ‘1 month’)::date and d.date <= r.date ),
//又将副表1与原表left join得到的表作为副表2
hb_1 as(
select a.category_1,
//最近一个月的quantity
sum(case when a.date = e.date then a.quantity end) as current_quantity,
//上一个月的quantity,如果求同比的话,将’1 month ’ 换成’12 month’
sum(case when (e.date > (a.date - interval ‘1 month’)::date and e.date <= a.date) then e.quantity end) as preview_quantity
from quantity a
left join every e
on a.category_1 = e.category_1
group by a.category_1
),
result as(
select category_1,(current_quantity - preview_quantity )/ preview_quantity as hb
from hb_1 )
select * from result ;
环比增长计算公式:(这个月的quantity-上个月的quantity)/上个月的quantity
同比增长计算公式:(这个月的quantity-去年同月的quantity)/去年同月的quantity
相关文章推荐
- 用MDX进行同比及环比计算
- 用MDX进行同比及环比计算(转)
- MDX同比环比的计算
- 同比和与环比的计算公式
- 用MDX计算同比、环比
- 用MDX计算同比、环比
- 用MDX进行同比及环比计算
- 同比和与环比的计算公式
- 同比和与环比的计算公式
- 增长或下降的百分比计算以及同比环比概念
- 同比和环比计算公式?
- 同比和环比的计算
- Oracle BIEE 中的时间维与时间轴函数,同期,同比,环比,累计,同期累计计算
- Oracle如何根据一个日期计算同比环比的日期
- 同比和与环比的计算公式
- MDX多维查询,环比和同比的计算
- 同比、环比的区别及计算公式
- sqlserver 计算同比,环比增长
- 5.4.2.1、SSAS-创建计算-同环比
- 10月百城房价同比环比首次双跌