当前位置:网站首页>展示用sql创建中间数据表的实际工作用例
展示用sql创建中间数据表的实际工作用例
2022-06-28 18:39:00 【南湖渔歌】
show databases;
show tables;
select * from bi_df_m bdm ;
# 环比
# 消费
select
-- substr(a.d,1,7) as month ,
STR_TO_DATE(a.d,'%Y-%m-%d') as 月份,
round(a.consume) as 消费,
-- b.dd,
-- b.original_month,
round(b.consume) as 环比上月, -- as consume_last,
round((a.consume-b.consume)/b.consume,2) as 环比率 -- 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
(
-- 运行正确
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;
# 注册
select
-- substr(a.d,1,7) as month ,
STR_TO_DATE(a.d,'%Y-%m-%d') as time,
round(a.reg) as 注册数,
-- b.dd,
-- b.original_month,
round(b.reg) as 环比上月, -- as reg_last,
round((a.reg-b.reg)/b.reg,2) as 环比增长率 -- 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
(
-- 运行正确
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;
# --------------------------------------- 通过r_year,r_month,构建新的字段 '年-月-01' ----------------------------------
desc grafana_bi.bi_df_m ; # 查看数据表字段:
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) # 新增字段
,`update_Time` varchar(50)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
# 查看数据表是否创建成功
SELECT * from grafana_bi.temp_bi_df_m limit 10;
# 插数
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 # 增加了一个 rm 字段
,update_Time
from grafana_bi.bi_df_m ;
show tables;
# 补充字段并创建新表成功:
select *
from grafana_bi.temp_bi_df_m ;
# -------------------------------------- 在新的底表temp_bi_df_m中开发grafana sql脚本 --------------------------------------
-- 消费
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 -- 将之前按照:r_year,r_month 分组 变成安装ym字段分组
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t ) a
left join
(
-- 运行正确
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
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 -- 将之前按照:r_year,r_month 分组 变成安装ym字段分组
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
(
-- 运行正确
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;
-- 入金:
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 -- 将之前按照:r_year,r_month 分组 变成安装ym字段分组
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
(
-- 运行正确
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;
#--------------------------------------------------- 开发适应grafana的分析图表sql语句--------------------------------------
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;
# 插数
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 -- 将之前按照:r_year,r_month 分组 变成安装ym字段分组
from temp_bi_df_m tb
where channel_kind = 'APP' AND brand_name = '2_bibgold'
group by ym) t ) a
left join
(
-- 运行正确
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脚本:
-- 法一:分单个query去写:
select STR_TO_DATE(ddate,'%Y-%m-%d') as time
,consume
,consume2
from grafana_bi.app_c_df_m;
-- 法二:分两个query去写:
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;
边栏推荐
猜你喜欢
随机推荐
MongoDB系列之MongoDB工作原理简单介绍
POI Excel转换工具
从理论到实践增强STEAM和工程教育
PHP使用栈解决迷宫问题
19.2 container classification, array and vector container refinement
技术管理进阶——管理者如何做绩效沟通及把控风险
手动备份和还原DHCP服务器
postgresql数据库docker
ANR Application Not Responding
Object tracking using tracker in opencv
What are the design requirements for PCB layout and wiring?
基于管线的混合渲染
牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络
安装nodejs环境
devpi
向上转型和向下转型
POI excel conversion tool
【C#】详解值类型和引用类型区别
数字化转型的1个目标,3大领域,6大因素和9个环节
async-validator. JS data verifier








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