当前位置:网站首页>Show the actual work case of creating intermediate data table with SQL
Show the actual work case of creating intermediate data table with SQL
2022-06-28 19:01:00 【South Lake Fishing Song】
show databases;
show tables;
select * from bi_df_m bdm ;
# Chain ratio
# consumption
select
-- substr(a.d,1,7) as month ,
STR_TO_DATE(a.d,'%Y-%m-%d') as month ,
round(a.consume) as consumption ,
-- b.dd,
-- b.original_month,
round(b.consume) as Month on month , -- as consume_last,
round((a.consume-b.consume)/b.consume,2) as Link ratio -- month_rate
from
(
select DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d') as d,consume
from
(
select r_year,r_month,sum(consume) as consume
from grafana_bi.bi_df_m bdm
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by r_year,r_month) t
) a
left join
(
-- Run right
select DATE_ADD(DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d'),INTERVAL 1 month ) as dd
,DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d') as original_month
,consume
from
(
select r_year,r_month,sum(consume) as consume
from grafana_bi.bi_df_m bdm
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by r_year,r_month) t
) b
on a.d = b.dd;
# register
select
-- substr(a.d,1,7) as month ,
STR_TO_DATE(a.d,'%Y-%m-%d') as time,
round(a.reg) as Number of registrations ,
-- b.dd,
-- b.original_month,
round(b.reg) as Month on month , -- as reg_last,
round((a.reg-b.reg)/b.reg,2) as Month on month growth rate -- month_rate
from
(
select DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d') as d,reg
from
(
select r_year,r_month,sum(reg) as reg
from grafana_bi.bi_df_m bdm
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by r_year,r_month) t
) a
left join
(
-- Run right
select DATE_ADD(DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d'),INTERVAL 1 month ) as dd
,DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d') as original_month
,reg
from
(
select r_year,r_month,sum(reg) as reg
from grafana_bi.bi_df_m bdm
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by r_year,r_month) t
) b
on a.d = b.dd;
# --------------------------------------- adopt r_year,r_month, Build a new field ' year - month -01' ----------------------------------
desc grafana_bi.bi_df_m ; # Look at the data table fields :
use grafana_bi;
DROP TABLE IF EXISTS `temp_bi_df_m`;
CREATE TABLE IF NOT EXISTS `temp_bi_df_m`(
`r_year` int(10)
,`r_month` varchar(10)
,`channel_kind` varchar(10)
,`brand_name` varchar(10)
,`consume` int(10)
,`flow` int(10)
,`ent` int(10)
,`reg` int(10)
,`r_quarter` varchar(10)
,`ym` varchar(10) # New fields
,`update_Time` varchar(50)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
# Check whether the data table was created successfully
SELECT * from grafana_bi.temp_bi_df_m limit 10;
# Interpolation number
insert into grafana_bi.temp_bi_df_m(r_year,r_month,channel_kind,brand_name,consume,flow,ent,reg,r_quarter,ym,update_Time)
select
r_year
,r_month
,channel_kind
,brand_name
,consume
,flow
,ent
,reg
,r_quarter
,DATE_FORMAT(CONCAT(r_year,'-',r_month,'-01'),'%Y-%m-%d') as ym # Added one rm Field
,update_Time
from grafana_bi.bi_df_m ;
show tables;
# Adding fields and creating a new table succeeded :
select *
from grafana_bi.temp_bi_df_m ;
# -------------------------------------- In the new bottom table temp_bi_df_m In the development grafana sql Script --------------------------------------
-- consumption
select
STR_TO_DATE(a.d,'%Y-%m-%d') as time,
a.consume as consume ,
b.consume as consume_last,
round((a.consume-b.consume)/b.consume,2) as month_rate
from
(
select ym as d,consume
from (select ym,sum(consume) as consume -- According to :r_year,r_month grouping Become installation ym Field grouping
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t ) a
left join
(
-- Run right
select DATE_ADD(ym,INTERVAL 1 month ) as dd
,ym as original_month
,consume
from
(
select ym,sum(consume) as consume
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t
) b
on a.d = b.dd;
-- register
select
STR_TO_DATE(a.d,'%Y-%m-%d') as time,
a.reg as reg ,
b.reg as reg_last,
round((a.reg-b.reg)/b.reg,2) as month_rate
from
(
select ym as d,reg
from (select ym,sum(reg) as reg -- According to :r_year,r_month grouping Become installation ym Field grouping
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
-- where channel_kind in ($channel_kind) AND brand_name in ($brand_name)
group by ym) t ) a
left join
(
-- Run right
select DATE_ADD(ym,INTERVAL 1 month ) as dd
,ym as original_month
,reg
from
(
select ym,sum(reg) as reg
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
-- where channel_kind in ($channel_kind) AND brand_name in ($brand_name)
group by ym) t
) b
on a.d = b.dd;
-- Into gold :
select
STR_TO_DATE(a.d,'%Y-%m-%d') as time,
a.ent as ent ,
b.ent as ent_last,
round((a.ent-b.ent)/b.ent,2) as month_rate
from
(
select ym as d,ent
from (select ym,sum(ent) as ent -- According to :r_year,r_month grouping Become installation ym Field grouping
from temp_bi_df_m tb
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
where channel_kind in ($channel_kind) AND brand_name in ($brand_name)
group by ym) t ) a
left join
(
-- Run right
select DATE_ADD(ym,INTERVAL 1 month ) as dd
,ym as original_month
,ent
from
(
select ym,sum(ent) as ent
from temp_bi_df_m tb
-- where channel_kind = 'APP' AND brand_name = '2_bibgold'
where channel_kind in ($channel_kind) AND brand_name in ($brand_name)
group by ym) t
) b
on a.d = b.dd;
#--------------------------------------------------- Development adapts to grafana Analysis chart of sql sentence --------------------------------------
use grafana_bi;
DROP TABLE IF EXISTS `app_c_df_m`;
CREATE TABLE IF NOT EXISTS `app_c_df_m`(
`ddate` varchar(50)
,`consume` int(10)
,`consume2` int(10)
-- ,`month_rate` decimal(10,3)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
select *
from app_c_df_m;
# Interpolation number
insert into grafana_bi.app_c_df_m(ddate,consume,consume2)
select
a.d as ddate,
a.consume as consume ,
b.consume as consume_last
-- round((a.consume-b.consume)/b.consume,2) as month_rate
from
(
select ym as d,consume
from (select ym,sum(consume) as consume -- According to :r_year,r_month grouping Become installation ym Field grouping
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t ) a
left join
(
-- Run right
select DATE_ADD(ym,INTERVAL 1 month ) as dd
,ym as original_month
,consume
from
(
select ym,sum(consume) as consume
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t
) b
on a.d = b.dd;
SELECT * from grafana_bi.app_c_df_m;
# grafana sql Script :
-- Law 1 : Individual query To write :
select STR_TO_DATE(ddate,'%Y-%m-%d') as time
,consume
,consume2
from grafana_bi.app_c_df_m;
-- Law two : In two query To write :
select STR_TO_DATE(ddate,'%Y-%m-%d') as time
,consume
from grafana_bi.app_c_df_m;
select STR_TO_DATE(ddate,'%Y-%m-%d') as time
,consume2
from grafana_bi.app_c_df_m;
边栏推荐
- CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
- 中金财富开户安全吗?开过中金财富的讲一下
- 微软独家付费功能,也被完美解锁了
- Collection of real test questions
- 内存泄露
- OOM out of memory 内存溢出
- About Significance Tests
- Business layer modification - reverse modification based on the existing framework
- OpenHarmony—内核对象事件之源码详解
- MindSpore系列一加载图像分类数据集
猜你喜欢
随机推荐
MindSpore系列一加载图像分类数据集
19.2 container classification, array and vector container refinement
Openharmony - detailed source code of Kernel Object Events
百度时间因子添加
618 activity season - the arrival of special discounts for hundreds of low code platforms
About Critical Values
openGauss内核:SQL解析过程分析
Object tracking using tracker in opencv
Openfire 3.8.2集群配置
Native implementation Net5.0+ custom log
深入解析kubernetes中的选举机制
[C #] explain the difference between value type and reference type
Taishan Office Technology Lecture: word strange font height
⼤⼚⾯试真题集合
业务层修改--根据现有框架的反推修改
Understanding of closures
POI Excel转换工具
Oom out of memory memory overflow
tensorboard 使用总结
async-validator.js数据校验器









![[unity3d] camera follow](/img/11/6309450f2b3ef33df558104549dc4c.png)