SQL;具有计数值的百分比分布

时间:2015-03-20 15:43:23

标签: sql sql-server count distribution percentage

我有一张桌子(工资),工资分配在性别和工作标准上。 我想显示我在数据库中的每个职位以及总价值的性别结果。例如,对于第一个标题,我有这个代码:

select 'Female' as '____',
       count(gender) as Number,
       SUM(Salary) AS Total_Salary,
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries
where gender= 'Female'
  And Job_titel='Adm assistent'
union (
select 'Man' as '____',
       count(gender) as Number,
       SUM(Salary) AS Total_Salary,
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries
where gender= 'Man'
  AND Job_titel='Adm assistent')
union (
select 'Total' as '____',
       count(gender) as Number,
       SUM(Salary) AS Total_Salary,
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries
where Job_titel ='Adm assistent' )

表格:

  ____  Number Tot_Salaries Min_Salaries Avg_Salaries Max_Salaries
----------------------------------------------------------

Female    142      1 000 000          XXXXXX         XXXXXXX      XXXXXXX  
Male       18        300 000          XXXXXX         XXXXXXX      XXXXXXX
Total     160      1 300 000          XXXXXX         XXXXXXX      XXXXXXX
----------

现在我希望表格的性别分布为%,如下所示:

   ___  Number %_Gender Tot_Salaries %  Min_Salaries Avg_Salaries Max_Salaries

Female    142    89     1 000 000   77   XXXXXX      XXXXXXX     XXXXXXX  
Male       18    11       300 000   13   XXXXXX      XXXXXXX     XXXXXXX
Total     160   100     1 300 000   00   XXXXXX     XXXXXXX      XXXXXXX
----------

如何获得这两个分布值?我认为我的代码现在可能非常无效:-)我使用的是MS sql server。

2 个答案:

答案 0 :(得分:0)

您可以将2个声明的变量用于totalcount和totalsalary

declare @totalgender  decimal(12,1)
declare @totalSalary  decimal(12,1)

select @totalgender=(select count(gender) as total from Salaries where Job_titel ='Adm assistent' )
select @totalSalary=(select SUM(Salary) as total from Salaries where Job_titel ='Adm assistent' )

select 'Female' as '____',
       count(Salaries.gender) as Number, cast(count(Salaries.gender) / @totalgender*100 as int),
       SUM(Salary) AS Total_Salary, cast(SUM(Salary) / @totalSalary*100 as int),
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries   
where gender= 'Female'
  And Job_titel='Adm assistent'
union (
select 'Man' as '____',
       count(gender) as Number, cast(count(Salaries.gender) / @totalgender*100  as int),
       SUM(Salary) AS Total_Salary, cast(SUM(Salary) / @totalSalary*100 as int),
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries
where gender= 'Man'
  AND Job_titel='Adm assistent')
union (
select 'Total' as '____',
       count(gender) as Number, cast(count(Salaries.gender) / @totalgender*100  as int),
       SUM(Salary) AS Total_Salary, cast(SUM(Salary) / @totalSalary*100 as int),
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
from Salaries
where Job_titel ='Adm assistent' )
  

或使用select (select Job_titel, cast(count(gender )as decimal(5,2)) as total from Salaries where Job_titel ='Adm assistent' group by Job_titel) stot

连接表格
select 'Female' as '____',
       count(s.gender) as Number, max(stot.total),
          cast(count(s.gender)/max(stot.total)*100 as int),
       SUM(s.Salary) AS Total_Salary,  
       MIN(s.salary) AS Min_salary,
       AVG(s.salary) AS Avg_Salary,
       MAX(s.salary) AS Max_Salary
from Salaries s inner join 
    (select Job_titel, cast(count(gender )as decimal(5,2)) as total from Salaries where Job_titel ='Adm assistent'  group by Job_titel) stot 
    on s.Job_titel= stot.Job_titel 
where gender= 'Female'
  And s.Job_titel='Adm assistent'
union  
select 'Man' as '____',
       count(s.gender) as Number, max(stot.total),
          cast(count(s.gender)/max(stot.total)*100 as int),
       SUM(s.Salary) AS Total_Salary,  
       MIN(s.salary) AS Min_salary,
       AVG(s.salary) AS Avg_Salary,
       MAX(s.salary) AS Max_Salary
from Salaries s inner join 
    (select Job_titel, cast(count(gender )as decimal(5,2)) as total from Salaries where Job_titel ='Adm assistent'  group by Job_titel) stot 
    on s.Job_titel= stot.Job_titel 
where gender= 'Man'
  And s.Job_titel='Adm assistent'  
union  
select 'Total' as '____',
       count(s.gender) as Number, max(stot.total),
          cast(count(s.gender)/max(stot.total)*100 as int),
       SUM(s.Salary) AS Total_Salary,  
       MIN(s.salary) AS Min_salary,
       AVG(s.salary) AS Avg_Salary,
       MAX(s.salary) AS Max_Salary
from Salaries s inner join 
    (select Job_titel, cast(count(gender )as decimal(5,2)) as total from Salaries where Job_titel ='Adm assistent'  group by Job_titel) stot 
    on s.Job_titel= stot.Job_titel 
where S.Job_titel ='Adm assistent'  

答案 1 :(得分:0)

这将更加优化。无需多次从Salaries表中查询。

SELECT Job_title,gender,
        count(gender) as Number,
       SUM(salary) AS Total_Salary,
       MIN(salary) AS Min_salary,
       AVG(salary) AS Avg_Salary,
       MAX(salary) AS Max_Salary
   INTO #t
FROM Salaries
WHERE Job_title ='Adm assistent'
GROUP BY Job_title, gender

SELECT t.Job_title, t.gender, t.Number, 
        CASE WHEN t2.tot_num = 0 THEN 0 ELSE  t.Number/CAST(t2.tot_num AS float) * 100 END as [pct_gender],
        t.Total_Salary, t.Min_salary, t.Avg_Salary, t.Max_Salary
FROM #t t
    INNER JOIN  (SELECT Job_title, SUM(Number) AS tot_num
                FROM #t
                GROUP BY Job_title) t2 
        ON t2.Job_title = t.Job_title 
UNION   
SELECT t.Job_title, 'Total', SUM( t.Number),    
        100 as [pct_gender],
        SUM(t.Total_Salary), SUM(t.Min_salary) , SUM(t.Avg_Salary), SUM( t.Max_Salary)
FROM #t t
GROUP BY t.Job_title
ORDER BY Job_title,Number

DROP TABLE #t