当前位置:   article > 正文

展示用sql创建中间数据表的实际工作用例_sql中间表使用方法

sql中间表使用方法

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;

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/635354
推荐阅读
相关标签
  

闽ICP备14008679号