当前位置:网站首页>An example of data format conversion in MySQL
An example of data format conversion in MySQL
2022-06-21 23:11:00 【atwdy】
background
A requirement encountered in the work , Put the data in this storage form ( They are all test data and do not contain any sensitive information ),
Return... In the following form ,
According to the table put_sell Field to determine whether to put or sell ,0- Put on the 1- sell
For daily data , The above is the most complete case , Or maybe every asset_scale Only sell or put , Or maybe not , Like this 
Method 1
First thought of this idea , Simple but flawed , Just look at the code
with t1 as (
select
asset_scale,
(case put_sell when 0 then last_week else 0 end) as last_week_put,
(case put_sell when 0 then current_week else 0 end) as current_week_put,
(case put_sell when 1 then last_week else 0 end) as last_week_sell,
(case put_sell when 1 then current_week else 0 end) as current_week_sell
from book_management.weekly_asset_put_sell where reportDateId = 20220616
)
select
asset_scale,
sum(last_week_put) as last_week_put,
sum(current_week_put) as current_week_put,
sum(last_week_sell) as last_week_sell,
sum(current_week_sell) as current_week_sell
from t1 group by asset_scale;
Execution results :
What defects can be seen here , It should have been null null All the places have returned 0, The reason is because mysql Medium sum Sum up null It will not cause spread , That is to say null Will be ignored . To be frank ,sum If the partial value in the column is null, It ignores null, Return non null Sum of values , If sum The values of all columns are null, Then return to null.
According to this feature , The above code only needs a small change to meet the requirements ,
with t1 as (
select
asset_scale,
(case put_sell when 0 then last_week else null end) as last_week_put,
(case put_sell when 0 then current_week else null end) as current_week_put,
(case put_sell when 1 then last_week else null end) as last_week_sell,
(case put_sell when 1 then current_week else null end) as current_week_sell
from book_management.weekly_asset_put_sell where reportDateId = 20220616
)
select
asset_scale,
sum(last_week_put) as last_week_put,
sum(current_week_put) as current_week_put,
sum(last_week_sell) as last_week_sell,
sum(current_week_sell) as current_week_sell
from t1 group by asset_scale;
Also is to case when In the sentence else hinder 0 Replacing the null, Execution results :
The above code can be further simplified :
select
asset_scale,
abs(sum((case put_sell when 0 then last_week else null end))) as last_week_put,
abs(sum((case put_sell when 0 then current_week else null end))) as current_week_put,
abs(sum((case put_sell when 1 then last_week else null end))) as last_week_sell,
abs(sum((case put_sell when 1 then current_week else null end))) as current_week_sell
from book_management.weekly_asset_put_sell where reportDateId = 20220616 group by asset_scale;
Method 2
The idea is to filter out the release and sale and make them into a separate table , The two tables are based on asset_scale Make full connection , Only in the process of implementation do we know mysql Is not yet supported in full join This fully connected operation , It can be used left join+right join+union To achieve the same effect .
with t1 as (
select
asset_scale,
last_week as last_week_put,
current_week as current_week_put
from book_management.weekly_asset_put_sell where reportDateId = 20220616 and put_sell = 0
),
t2 as (
select
asset_scale,
last_week as last_week_sell,
current_week as current_week_sell
from book_management.weekly_asset_put_sell where reportDateId = 20220616 and put_sell = 1
)
select
t1.asset_scale,
last_week_put,
current_week_put,
last_week_sell,
current_week_sell
from t1 left join t2 on t1.asset_scale = t2.asset_scale
union
select
t1.asset_scale,
last_week_put,
current_week_put,
last_week_sell,
current_week_sell
from t2 left join t1 on t1.asset_scale = t2.asset_scale;
Execution results :
This kind of thinking is more complicated than the first one , If you can understand the full connection well, you will have no other problems .
边栏推荐
- 原生小程序 申请小程序 - 发布流程
- mvn deploy多个模块的bat文件
- MVN deploy bat files for multiple modules
- How to adjust the resolution of the computer screen? Computer screen modification resolution switchresx
- mysql中数据格式转换的一个示例
- #CISSP认证2021年教材 OSG 第9版 增(改)知识点:与 第8版 的目录对比
- What is the most challenging issue in Bi development?
- C # error: the exception of the task is not observed by waiting for the task or accessing the exception attribute of the task. As a result, the finalizer thread re threw an unobserved exception.
- WPF combobox setting options and anti display
- Uni app advanced style framework / production environment [Day10]
猜你喜欢

WPF thread manipulation UI problem

Project process management tool OmniPlan Pro 4

Apache shardingsphere 5.1.2 release | new driving API + cloud native deployment to create a high-performance data gateway

《MATLAB 神经网络43个案例分析》:第9章 离散Hopfield神经网络的联想记忆——数字识别

并查集练习题1:朋友圈

Analysis of 43 cases of MATLAB neural network: Chapter 9 associative memory of discrete Hopfield Neural Network -- number recognition

WPF dependent properties

How to adjust the resolution of the computer screen? Computer screen modification resolution switchresx

Matlab2020a how to export exe using app Designer

初识 vxe-table (一)
随机推荐
Text text associated with scroll scroll bar in Tkinter
378. 有序矩阵中第 K 小的元素-常规法
Using JS function in wxml file of applet
uniapp封装request函数 实现唯一登录,一个账号同时只能登陆一个设备
C# 报错:未通过等待任务或访问任务的 Exception 属性观察到任务的异常。因此,终结器线程重新引发了未观察到的异常。
Design summary of Verilog divider
About LG (n!) Asymptotically compact supremum of
[WUSTCTF2020]朴实无华-1
#CISSP认证2021年教材 OSG 第9版 增(改)知识点:与 第8版 的目录对比
[understanding pointer] advanced level of pointer
Design and implementation of spark offline development framework
花200W买流量,不如0成本起步做独立站私域运营收益高!
[wustctf2020] plain and unpretentious -1
H5之微信授权登陆 (uniapp网页版微信授权登录)
Contracting of development environment and test environment (and request encapsulation of uniapp)
WPF tablet
Use of three values of pointer events
《MATLAB 神经网络43个案例分析》:第19章 基于SVM的手写字体识别
Better manage all kinds of music, professional DJ music management software pioneer DJ rekordbox
在商业智能BI开发过程中,什么问题的挑战性最大?