这个问题在这里已经有答案了:
在sql计算中使用别名(4个答案)
两年前关门了。
希望将returned和total列分开,以获得退货最多的产品的退货率。下面是我的例子,如何在别名上添加除法函数?
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;
谢谢
2条答案
按热度按时间wbrvyc0a1#
你可以把它包在另一个里面
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;
js81xvg62#
你可以这么做。