当前位置:网站首页>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;
边栏推荐
- C语言指针的一些易错点
- ANR Application Not Responding
- 【Unity3D】发射(RayCast)物理射线(Ray)
- An in-depth analysis of the election mechanism in kubernetes
- Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
- Servlet的使用手把手教学(一)
- memory thrashing
- First day of new work
- 被315点名的流氓下载器,又回来了…
- AOSP清华镜像下载错误解决
猜你喜欢

内存泄露

用户网络模型与QoE

Advanced - Introduction to business transaction design and development

Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour

Question brushing analysis tool

About Statistical Distributions

About Covariance and Correlation(协方差和相关)

基于管线的混合渲染

POI excel conversion tool

Cvpr2022 | Zhejiang University and ant group put forward a hierarchical residual multi granularity classification network based on label relation tree to model hierarchical knowledge among multi granu
随机推荐
About Critical Values
使用Karmada实现Helm应用的跨集群部署
微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
G 双轴图sql脚本
Openfire 3.8.2集群配置
⼤⼚⾯试真题集合
Sound network releases lingfalcon Internet of things cloud platform, which can build sample scenarios in one hour
shell读取Json文件的值
微博评论的高性能高可用计算架构方案
Tensorboard Usage Summary
Modular operation
About Critical Values
Idea merge other branches into dev branch
Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation
Advanced technology management - how managers communicate performance and control risks
sqrt()函数的详解和用法「建议收藏」
解析机器人主持教学的实践发展
Analyzing the practical development of robot teaching
泰山OFFICE技术讲座:WORD奇怪的字体高度
Shell unknown rollup 1