具有平均值和计数 SQL 的子查询
Subquery with average and count in SQL
亲爱的,
我的数据包含以下信息;客户ID、交易金额和交易对手地区。对于每个地区,我想计算交易量的平均值。我的想法是使用子查询,但我坚持了下来。这是我目前的想法,不可行。
select avg(count_for_DK) from data where
(select count(amount) as count_for_DK, customer_id from data
where country = 'DK'
group by customer_id, country
order by customer_id asc)
有人帮帮我吗?
我的 table 示例屏幕:
https://ibb.co/BZXDL4Q
根据我的理解,您想获得国家/地区 ='DK' 的客户明智的平均金额
如果这个逻辑是你正在寻找的,那么下面的查询将起作用:
select customer_id ,avg(amount) as average_for_DK from data
where country = 'DK'
group by customer_id
order by customer_id
如果您想获得'DK'
国家/地区的客户完成的平均交易次数
select avg(count_for_DK) from (
select customer_id ,count(amount) as count_for_DK from data
where country = 'DK'
group by customer_id
order by customer_id) t
如果您想获得每个国家/地区的客户完成的平均交易次数:
select country,avg(count_for_country) from (
select country,customer_id ,count(amount) as count_for_country from data
group by country,customer_id
order by country,customer_id) t
group by country
亲爱的, 我的数据包含以下信息;客户ID、交易金额和交易对手地区。对于每个地区,我想计算交易量的平均值。我的想法是使用子查询,但我坚持了下来。这是我目前的想法,不可行。
select avg(count_for_DK) from data where
(select count(amount) as count_for_DK, customer_id from data
where country = 'DK'
group by customer_id, country
order by customer_id asc)
有人帮帮我吗?
我的 table 示例屏幕: https://ibb.co/BZXDL4Q
根据我的理解,您想获得国家/地区 ='DK' 的客户明智的平均金额 如果这个逻辑是你正在寻找的,那么下面的查询将起作用:
select customer_id ,avg(amount) as average_for_DK from data
where country = 'DK'
group by customer_id
order by customer_id
如果您想获得'DK'
国家/地区的客户完成的平均交易次数select avg(count_for_DK) from (
select customer_id ,count(amount) as count_for_DK from data
where country = 'DK'
group by customer_id
order by customer_id) t
如果您想获得每个国家/地区的客户完成的平均交易次数:
select country,avg(count_for_country) from (
select country,customer_id ,count(amount) as count_for_country from data
group by country,customer_id
order by country,customer_id) t
group by country