当前位置:网站首页>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 ),
 Insert picture description here
Return... In the following form ,
 Insert picture description here
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
 Insert picture description here

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 :
 Insert picture description here

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 :
 Insert picture description here
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 :
 Insert picture description here

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 .

原网站

版权声明
本文为[atwdy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206212121354626.html