Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在T-SQL中,按地区查找%男性客户的最佳方法是什么_Sql_Tsql_Aggregate Functions - Fatal编程技术网

在T-SQL中,按地区查找%男性客户的最佳方法是什么

在T-SQL中,按地区查找%男性客户的最佳方法是什么,sql,tsql,aggregate-functions,Sql,Tsql,Aggregate Functions,支持我有一个表,上面有区域、客户和客户的性别信息,我想找出每个区域中男性客户的百分比。最好的办法是什么 create table temp(area_id varchar(10),customer_id varchar(10),customer_sex varchar(10)) insert into temp select 1,1,'male' insert into temp select 1,1,'male' insert into temp select 1,1,'female' in

支持我有一个表,上面有区域、客户和客户的性别信息,我想找出每个区域中男性客户的百分比。最好的办法是什么

create table temp(area_id  varchar(10),customer_id varchar(10),customer_sex varchar(10))
insert into temp select 1,1,'male'
insert into temp select 1,1,'male'
insert into temp select 1,1,'female'
insert into temp select 1,1,'female'
insert into temp select 2,1,'male'
insert into temp select 2,1,'female'
insert into temp select 2,1,'female'
insert into temp select 3,1,'male'
insert into temp select 3,1,'female'
insert into temp select 4,1,'male'
insert into temp select 5,1,'female'

select * from temp
结果如下:

这样做可以:

select x.area_id, x.total,  x.m, cast(CONVERT(DECIMAL(10,2), x.m * 100.0 / x.total) as nvarchar(max)) + '%'
from
(
    select t.area_id, count(1) total, sum(iif(t.customer_sex = 'male', 1, 0)) m
    from @temp t
    group by t.area_id
)x
使用IIF(Sqlserver 2012+),否则按案例、分组和男性总数/统计所有客户*100 +0.0将男性和所有客户的总和视为浮点数或小数,以获得正确的结果

select area_id,count(customer_id) [Total Customers],
sum(iif(customer_sex='male',1,0)) [Total Males],
cast(cast(((sum(iif(customer_sex='male',1,0)) + 0.0) / (count(customer_sex) + 0.0)) * 100 as decimal(18,1)) as varchar(10)) + '%' [percentage of males]
from temp
group by area_id

分组和案例将提供您的结果:

SELECT area_id, count(customer_id) as Total_Customers, Total_Male_Customers = sum(case when customer_sex = 'male' then 1 else 0 end),
    Format(sum(case when customer_sex = 'male' then 1 else 0 end)/(count(customer_id)*1.0),'P') as MaleCustomers 
FROM dbo.temp
GROUP BY area_id
HAVING sum(case when customer_sex = 'male' then 1 else 0 end) > 0
在这里,如果数据集格式更小,则更好,否则会出现性能问题,您可以使用自定义乘法和连接%symbol

输出如下:

+---------+-----------------+----------------------+---------------+
| area_id | Total_Customers | Total_Male_Customers | MaleCustomers |
+---------+-----------------+----------------------+---------------+
|       1 |               4 |                    2 | 50.00 %       |
|       2 |               3 |                    1 | 33.33 %       |
|       3 |               2 |                    1 | 50.00 %       |
|       4 |               1 |                    1 | 100.00 %      |
+---------+-----------------+----------------------+---------------+

获取女性计数并划分哪个sql数据库?您需要使用“GROUPBY”子句。我认为这应该可以做到。。从temp group by area_id中选择area_id,male_customer_count=count(当customer_sex='male'然后是'1'结束时的情况)*100.0/count(*)您真的需要使用派生表来获取MALER的数量吗?
+---------+-----------------+----------------------+---------------+
| area_id | Total_Customers | Total_Male_Customers | MaleCustomers |
+---------+-----------------+----------------------+---------------+
|       1 |               4 |                    2 | 50.00 %       |
|       2 |               3 |                    1 | 33.33 %       |
|       3 |               2 |                    1 | 50.00 %       |
|       4 |               1 |                    1 | 100.00 %      |
+---------+-----------------+----------------------+---------------+