希望将“返回”列和“总计”列分开,以获得最多返回产品的返回率。这是我所拥有的,如何在别名上添加除法函数?
SELECT brand
,model
,count(*) Total
,sum(case when returned = 'Y' then 1 else 0 end) as Returned
,sum(case when returned = '' then 1 else 0 end) as Kept
FROM table
WHERE year= '2018'
AND NOT type = 's'
GROUP by model
ORDER by Returned DESC;
谢谢
最佳答案
您可以将其包装在另一个SELECT
中
SELECT brand
,model
,count(*) Total
,sum(case when returned = 'Y' then 1 else 0 end) as Returned
,sum(case when returned = '' then 1 else 0 end) as Kept
,(SELECT Returned/Total) as Rate
FROM table
WHERE year= '2018'
AND NOT type = 's'
GROUP by model
ORDER by Returned DESC;
关于mysql - 根据别名 : SQL 进行划分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48069532/