公告
财富商城
积分规则
提问
发文
2020-05-09 18:21发布
干净又极端
What percent of customers are reward members?
ID Reward Member 1 y 2 y 3 n 4 y 5 n
I would use conditional aggregation:
select avg( case when rewardmember = 'y' then 1.0 else 0 end) as ratio from t;
Some databases allow shorter syntax, such as:
select avg( rewardmember = 'y' ) from t;
or:
select avg( (rewardmember = 'y')::int ) from t;
You can use COUNT():
COUNT()
select 1.0 * count(case when reward_member = 'y' then 1 else 0 end) / count(*) from t
Count how many are members, divide by total number of customers and multiply by 100.
SELECT sum(case when RewardMember = 'y' then 1 else 0 end)*100.0/count(*) as percentage FROM Customers
最多设置5个标签!
I would use conditional aggregation:
Some databases allow shorter syntax, such as:
or:
You can use
COUNT()
:Count how many are members, divide by total number of customers and multiply by 100.