我正在尝试查询以告诉我数据集中男性和女性的百分比(我之前提出的问题的扩展)。查询仅来自一张表。
declare @totalPop int;
declare @totalmales int;
declare @totalFemales int;
declare @percentMales float;
declare @percentFemales float;
set @totalpop =(select count(*) from myTable)
set @totalmales =(select count(*) from myTable where (sex='m'))
set @totalfemales =(select count(*) from myTable where (sex='f'))
set @percentMales=@totalMales/totalPop
set @percenFemales =@totalFemales/totalPop
select sex, count(sex), @totalPop, @totalMales, @totalFemales
我知道目前这行不通,但我不知道如何在一行上除以 totalMales,在下一行除以 totalFemales