学到很多丨如何取多个单独的数据再拼接起来
某宝店铺动销率与售罄率
https://www.nowcoder.com/practice/715dd44c994f45cb871afa98f1b77538?tpId=268&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26tab%3DSQL%25E7%25AF%2587%26topicId%3D268
解决的方法就是通过写多个临时表来解决如下:
多个临时表之间用逗号隔开就行了,记得带相同的主键以用来拼接
group by 1的意思是group by select后面的第1个字段
with a AS(
SELECT
style_id,
SUM(inventory)'inventorytotal',
SUM(tag_price*inventory)'inventorymoney'
FROM product_tb
GROUP BY 1) ,
b AS(
SELECT
style_id,
SUM(sales_num)'sales_numtotal',
SUM(sales_price)'gmv'
FROM product_tb
JOIN sales_tb USING (item_id)
GROUP BY 1)
SELECT
style_id,
ROUND(sales_numtotal/(inventorytotal-sales_numtotal)*100,2)'pin_rate(%)',
ROUND(gmv/inventorymoney*100,2)'sell-through_rate(%)'
FROM a JOIN b USING(style_id)
GROUP BY 1