查询以获取居住在特定区域内的客户数量的百分比
Query to get percentage of how many customers live within a certain area
我有一个 x,y 坐标客户 table,我需要找出居住在某个区域内或不在某个区域内的客户的百分比,即 (50,50)
查询测试数据中小于 50 的任何人:
SELECT X_Coord, Y_Coord
from customerlocation
count where (X_Coord < 50 or Y_Coord < 50)
50 岁的任何人:
SELECT X_Coord, Y_Coord
from customerlocation
count where (X_Coord = 50 or Y_Coord = 50)
如何对这些查询进行分组以获得生活在 (50,50) 范围内和生活在 (50,50) 范围内的人的百分比?
对每项要求进行单独计数并除以总计数
select (a.count / count(*)) * 100, (b.count / count(*)) * 100
from
(select count(*) as count
from customerlocation where x_coord < 50 or y_coord < 50) as a,
(select count(*) as count
from customerlocation where x_coord = 50 or y_coord = 50) as b,
customerlocation
MySql
要统计X,Y坐标值:
SELECT coord_name, count(*) total_count
FROM customerlocation
WHERE X_Coord < 50 OR (Y_Coord < 50)
GROUP BY X_Coord, Y_Coord
试试这个。
select
cast(sum(A.lessthan50) as float)/cast((sum(A.lessthan50) +
sum(A.anyoneat50)) as float)*100 lessthan50,
cast(sum(A.anyoneat50) as float)/cast((sum(A.lessthan50) +
sum(A.anyoneat50)) as float)*100 anyoneat50
from
(
select
case when (x < 50 or y < 50) then 1 else 0 end as lessthan50,
case when (x = 50 or y = 50) then 1 else 0 end as anyoneat50
from cordinates
) A
我认为这是最简单的方法:
select avg( (X_Coord = 50 or Y_Coord = 50) )
from customerlocation
where X_Coord <= 50 or Y_Coord <= 50;
这可能有点难看,所以这里有一个更详细的替代方案:
select sum( (X_Coord = 50 or Y_Coord = 50) ) / sum( (X_Coord < 50 or Y_Coord < 50) )
from customerlocation
where X_Coord <= 50 or Y_Coord <= 50;
我有一个 x,y 坐标客户 table,我需要找出居住在某个区域内或不在某个区域内的客户的百分比,即 (50,50)
查询测试数据中小于 50 的任何人:
SELECT X_Coord, Y_Coord
from customerlocation
count where (X_Coord < 50 or Y_Coord < 50)
50 岁的任何人:
SELECT X_Coord, Y_Coord
from customerlocation
count where (X_Coord = 50 or Y_Coord = 50)
如何对这些查询进行分组以获得生活在 (50,50) 范围内和生活在 (50,50) 范围内的人的百分比?
对每项要求进行单独计数并除以总计数
select (a.count / count(*)) * 100, (b.count / count(*)) * 100
from
(select count(*) as count
from customerlocation where x_coord < 50 or y_coord < 50) as a,
(select count(*) as count
from customerlocation where x_coord = 50 or y_coord = 50) as b,
customerlocation
MySql
要统计X,Y坐标值:
SELECT coord_name, count(*) total_count
FROM customerlocation
WHERE X_Coord < 50 OR (Y_Coord < 50)
GROUP BY X_Coord, Y_Coord
试试这个。
select
cast(sum(A.lessthan50) as float)/cast((sum(A.lessthan50) +
sum(A.anyoneat50)) as float)*100 lessthan50,
cast(sum(A.anyoneat50) as float)/cast((sum(A.lessthan50) +
sum(A.anyoneat50)) as float)*100 anyoneat50
from
(
select
case when (x < 50 or y < 50) then 1 else 0 end as lessthan50,
case when (x = 50 or y = 50) then 1 else 0 end as anyoneat50
from cordinates
) A
我认为这是最简单的方法:
select avg( (X_Coord = 50 or Y_Coord = 50) )
from customerlocation
where X_Coord <= 50 or Y_Coord <= 50;
这可能有点难看,所以这里有一个更详细的替代方案:
select sum( (X_Coord = 50 or Y_Coord = 50) ) / sum( (X_Coord < 50 or Y_Coord < 50) )
from customerlocation
where X_Coord <= 50 or Y_Coord <= 50;