当前位置:网站首页>mysql中数据格式转换的一个示例
mysql中数据格式转换的一个示例
2022-06-21 21:21:00 【atwdy】
背景
工作中遇到的一个需求,把这种存储形式的数据(都是测试数据也不包含什么敏感信息就直接截图了),
以下面这种形式返回,
根据表中put_sell字段判定是投放还是卖出,0-投放 1-卖出
对于每天的数据,上面这种是最全的情况,也可能每个asset_scale只有卖出或投放,也可能都没有,像下面这样
Method 1
最先想到这种思路,简单但有缺陷,直解看代码吧
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;
执行结果:
什么缺陷到这儿就能看出来了,就是本应该是空值null的地方都返回了0,原因是因为mysql中的sum求和null并不会引起传播,也就是null会被忽略掉。直白的说,sum列中如果部分值为null,则忽略null,返回非null值的求和结果,如果sum列的值全为null,则返回null。
针对这个特点,上面代码只需要改动一个很小的地方就可以符合需求了,
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;
也就是将case when语句中else后面的0全部换成null,执行结果:
上面代码还可以再精简一下:
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
思路是将投放与卖出各自过滤出来单独做成一个表,两个表再根据asset_scale做全连接,只不过实现过程中才知道mysql中暂时还不支持full join这种全连接操作,可以用left join+right join+union的方式实现相同效果。
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;
执行结果:
感觉这种思路比第一种复杂了一些,主要能理解好全连接这块就没其它问题了。
边栏推荐
- 左手代码,右手开源,开源路上的一份子
- Better manage all kinds of music, professional DJ music management software pioneer DJ rekordbox
- MySql踩坑记录
- 啊啊啊啊啊啊啊
- If you spend 200W to buy traffic, it is better to start at 0 cost and make an independent station with high private domain operation income!
- Go service platform project (I) design of database tables and use of gendry Library
- WPF 路由
- WPF 选择文件夹
- WPF 手写板
- WPF 依赖属性
猜你喜欢

语音断点检测(短时改进子带谱熵)

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

Games101 job 7- detailed explanation of implementation steps of multi thread speed up

Matlab2020a使用App Designer如何导出exe

Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关

Design and implementation of spark offline development framework

Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading

4. ESP8266通过OLED实时显示DHT11温湿度参数

解决opencv在pycharm中代码提示失效

Distributed database uses logical volume to manage storage expansion
随机推荐
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
WPF x:Static
使用云开发实现微信支付的具体方法
解决笔记本电脑(i)某个键的字母按不出来
WPF 启动带参数
Wechat applet obtains network status
MySql踩坑记录
WPF 依赖属性
libra白皮书
Six possible challenges when practicing Devops
WPF routing
numpy矩阵初等变换
Pychart can run normally, and pyinstaller package software reports fatal error
WPF listbox virtualization
小程序如何关联微信小程序二维码,并实现二码合一聚合
KVM虚拟机在线扩展磁盘 —— 筑梦之路
必读书籍
Go service platform project (I) design of database tables and use of gendry Library
[WUSTCTF2020]朴实无华-1
Livres obligatoires