当前位置:网站首页>展示用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;
边栏推荐
猜你喜欢
![[unity3d] camera follow](/img/11/6309450f2b3ef33df558104549dc4c.png)
[unity3d] camera follow

打破学科之间壁垒的STEAM教育

微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined

Chapter 2 processing files, cameras and GUI Cameo applications

Alist+RaiDrive 给电脑整个80亿GB硬盘

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

Lumiprobe ProteOrange 蛋白质凝胶染料说明书

Steam education to break the barriers between disciplines

leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)

About Critical Values
随机推荐
C语言指针的一些易错点
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
Mybayis之核心主件分析
FFmpeg学习总结
Opengauss kernel: analysis of SQL parsing process
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
【Unity3D】发射(RayCast)物理射线(Ray)
leetcode 1689. Partitioning into minimum number of deci binary numbers
About Statistical Distributions
MongoDB系列之MongoDB工作原理简单介绍
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
Openfire 3.8.2集群配置
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
Oom out of memory memory overflow
idea其他分支合并到dev分支
leetcode 1647. Minimum deletions to make character frequencies unique
原生实现.NET5.0+ 自定义日志
memory thrashing
注意!PMP紧急缓考今天就截止了!