希望将“返回”和“总计”列分开,以获得最多返回产品的退货率。这就是我所拥有的,如何在别名上添加除法函数?
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;
由于
答案 0 :(得分:1)
您可以将其包含在另一个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;
答案 1 :(得分:0)
你可以这样做。
SELECT brand
,model
,count(*) as Total
,sum(case when returned = 'Y' then 1 else 0 end) as Returned
,sum(case when returned = 'Y' then 1 else 0 end)/count(*) as returnRate
,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;