6 sets of codes and analysis of Niuke SQL big factory interview questions in a certain east mall

3. E-commerce scene (some east mall)

SQL168 calculates the monthly GMV in 2021 in the mall

select 
date_format(event_time,'%Y-%m') as month,
round(sum(total_amount),0) as GMV
from tb_order_overall
where year(event_time) ='2021' and status!=2
group by month
having GMV>100000
order by GMV

SQL169 Statistical indicators of each product with a return rate of less than 0.5 in October 2021

select
    product_id,
    round(sum(if_click)/count(*),3) as ctr,
    round(if(sum(if_click)>0,sum(if_cart)/sum(if_click),0),3) as cart_rate,
    round(if(sum(if_cart)>0,sum(if_payment)/sum(if_cart),0),3) as payment_rate,
    round(if(sum(if_payment)>0,sum(if_refund)/sum(if_payment),0),3) as refund_rate
from tb_user_event
where date_format(event_time,'%Y-%m')='2021-10'
group by product_id
having refund_rate<=0.5
order by product_id

SQL170 Gross profit rate of each product of a certain store and the overall gross profit rate of the store
(1. Why do the two queries above and below union all have parentheses?
If there are no parentheses, the system defaults to the last order by to be sorted after joining the two tables. In this way, the "store summary" will be placed in the last row.
2. Why is having profit_rate > 24.9 instead of having profit_rate > 24.9%
profit_rate is a number, only the outermost % is added to profit_rate, when it is inside () It is a number
3. Why is the formula for calculating the gross profit rate above and below the union all different? Once the upper and lower sides use the same gross profit rate derivation formula, an error will be reported? By
controlling the variable method,
the upper and lower gross profit rate calculation formulas are exchanged. It turns out that the test can be passed.
When the two gross rate calculation formulas are different, change the name profit_rate of the following gross rate to another name, and the test can also pass. When the
two gross rate calculation formulas are the same, change the name profit_rate of the following gross rate Changed to another name, the test cannot be passed.
This shows that the problem is not due to the formula, but may be in the usage rules of union all.
I guess, it may be that union all cannot connect two columns with the same calculation method.

(
select 
'店铺汇总' as product_id,
concat(round((1-sum(in_price*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
from tb_order_detail
join tb_order_overall using(order_id)
join tb_product_info using(product_id)
where date(event_time)>='2021-10-01'
and shop_id='901'
and status=1
)

union all

(
select 
product_id,
concat(round((sum((price-in_price)*cnt)/sum(price*cnt))*100,1),'%') as profit_rate
from tb_order_detail
join tb_order_overall using(order_id)
join tb_product_info using(product_id)
where date(event_time)>='2021-10-01'
and shop_id='901'
and status=1
group by product_id
having profit_rate > 24.9
order by product_id
)

SQL171 Top 3 products with the highest repurchase rate among snack products
(1. Aliases cannot be used as judgment conditions in where
https://blog.csdn.net/weixin_28007513/article/details/114770136
2. The difficulty of this question lies in how to determine which products It is the second time I bought it.
First, in the subquery, by clustering the products and customers, the customer corresponding to each product can be retrieved on the table with count.
Then use sum and if in the parent query, if purchased Two or more times, it is recorded as 1, otherwise it is recorded as 0, divided by the total number of rows, it is the repurchase rate of each product.)

select 
product_id,
round(sum(if(cnt>=2,1,0))/count(*),3) as repurchase_rate 
from(
    select 
    product_id,
    uid,
    count(uid) as cnt 
    from tb_order_detail
    left join tb_order_overall using(order_id)
    left join tb_product_info using(product_id)
    where datediff((select max(event_time) from tb_order_overall),event_time)<90 
    and tag='零食'
    group by product_id,uid
    ) as a
group by product_id
order by repurchase_rate desc,product_id 
limit 3

SQL172 New customer unit price and customer acquisition cost in October
(note: in sum(price*cnt) - sum(distinct total_amount) as diff, either sum or distinct cannot be removed, otherwise it will violate the SQL’s **”appear The fields after select (except the aggregated fields SUM, AVG, MAX, MIN) must be in the group "** rule.
If you want to remove sum and distinct and make total_amount separate from sum, you can put total_amount in group by After that, the code looks like this)

select 
	round(avg(a_amount),1) as avg_amount,
	round(avg(diff),1) as avg_cost
from(
    select
		order_id,
		avg(total_amount) as a_amount,
		sum(price*cnt) - sum(distinct total_amount) as diff
	from tb_order_overall
	left join tb_order_detail using(order_id)
	where month(event_time)='10' 
    and (uid,event_time) in (select uid,min(event_time) from tb_order_overall group by uid) 
    group by order_id
    )as a
select 
	round(avg(a_amount),1) as avg_amount,
	round(avg(diff),1) as avg_cost
from(
    select
		order_id,
		avg(total_amount) as a_amount,
		(sum(price*cnt) - total_amount) as diff
	from tb_order_overall
	left join tb_order_detail using(order_id)
	where month(event_time)='10' 
    and (uid,event_time) in (select uid,min(event_time) from tb_order_overall group by uid) 
    group by order_id,total_amount
    )as a

SQL173 The dynamic sales rate and slow sales rate in July during the 901 National Day of the store
(1. Connect the three tables through dt,
the dt of the t table and the t2 table are exactly the same, and
the t table and the t1 table can only be connected when there is a difference of 7 days 2.
The t table is used to limit the time conditions,
the t1 table is used to calculate the total number of products with orders, and
the t2 table is used to calculate the quantity of all products.
Finally, use a query table to combine the above three tables to find 3. When linking tables
, you can add restrictions to get the desired results. For details, refer to the t1 table.

with t as # 取日期 10-01 到 10-03
(select 
date(event_time) as dt
from tb_order_overall 
where date(event_time) between '2021-10-01' and '2021-10-03'),
 
t1 as # 901商店已成交订单中,每个下单日期里的 product_id
(select 
date(event_time) dt,
tod.product_id
FROM tb_order_detail tod 
JOIN tb_order_overall too ON tod.order_id = too.order_id and status = 1
JOIN tb_product_info tpi ON tod.product_id = tpi.product_id and shop_id = '901'),
                          
t2 as # 计算 每个日期下 901商店的 在售商品总数
(select 
date(event_time) as dt,
COUNT(DISTINCT product_id) as sum_product
FROM tb_product_info,tb_order_overall
where shop_id = '901'
group by dt)
 
select t.dt, 
round(count(distinct t1.product_id) / sum_product ,3) as sale_rate, 
ROUND(1- (count(distinct t1.product_id) / sum_product ),3) as unsale_rate
from t 
left join t1 ON datediff(t.dt,t1.dt) between 0 and 6 
JOIN t2 on t.dt = t2.dt
group by t.dt
ORDER by t.dt 

Guess you like

Origin blog.csdn.net/qq118640594X/article/details/127378445