简体   繁体   English

SQL查询可根据年龄段和

[英]SQL Query that returns data based on age range and

I have employee table with some data as I've attached in the pictures below and want to query it to find the average salary and percentage of employee on each age range. 我有一张雇员表,其中包含一些数据,如下图所示,我想对其进行查询以找到每个年龄段的平均工资和雇员百分比。 I have used the following query but I don't know how to include the avarage and percentage result. 我使用了以下查询,但是我不知道如何包括平均和百分比结果。 Any help please. 请帮忙。

SELECT emp.agerange as [age Range], count(emp.agerange) as [Number of employee on this age range]
FROM (
  SELECT CASE  
    WHEN age >60  THEN '60 And Up'
    WHEN age >=41 and age <=60 THEN '41-60'
    WHEN age >=21 and age <=40 THEN '21-40'
    WHEN age <=20 THEN '20 And Below' END as agerange
  from kin.dbo.Employee) emp
  group by emp.agerange 

我想要的表和结果

You could use a cte to create your age groups, joining in any additional tables with info you want to work with, such as LoanBalance or Salary. 您可以使用cte创建您的年龄组,将其与要使用的信息一起添加到任何其他表格中,例如LoanBalance或Salary。

WITH emp as (
      SELECT CASE  
        WHEN age >60  THEN '60 And Up'
        WHEN age >=41 and age <=60 THEN '41-60'
        WHEN age >=21 and age <=40 THEN '21-40'
        WHEN age <=20 THEN '20 And Below' END as agerange
      ,l.LoanBalance -- might not be the field you are looking for
      from kin.dbo.Employee
      left join Loan l
      on ??????) -- You decide the join condition between these two tables

SELECT emp.agerange as [age Range]
,count(*) as [Number of employee on this age range]
,count(*) / (SELECT COUNT(*) FROM emp) as pctAgeRange
,(SELECT SUM(LoanBalance) / COUNT(*) FROM emp) as avgLoanBalance
FROM emp
  group by emp.agerange 

Does this work? 这样行吗? I put your AgeRange function into a CTE. 我将您的AgeRange函数放入CTE中。

WITH cteRange AS (
    SELECT ID,
        CASE  
            WHEN age > 60  THEN '60 And Up'
            WHEN age >= 41 and age <=60 THEN '41-60'
            WHEN age >= 21 and age <=40 THEN '21-40'
            WHEN age <= 20 THEN '20 And Below'
        END AS 'ageRange'
    FROM kin.dbo.Employee
)
SELECT cteRange.ageRange,
    COUNT(*) AS 'Number of Employees',
    SUM(emp.Salary) AS 'Total Salary',
    AVG(emp.Salary) AS 'Average Salary',
    ROUND(COUNT(*)/(SELECT COUNT(*) FROM kin.dbo.Employee)*100,2) AS '% Employees in age Range'
FROM kin.dbo.Employee AS emp
    INNER JOIN cteRange ON emp.ID = cteRange.ID
GROUP BY cteRange.ageRange

No need for joins, the average is a simple SQL AVG and the percentage can easily be calculated using a GROUP SUM: 不需要联接,平均值是一个简单的SQL AVG,并且可以使用GROUP SUM轻松地计算百分比:

SELECT emp.agerange as [age Range],
   count(*) as [Number of employee on this age range],
   SUM(Salary) AS "Total Salary",  
   AVG(Salary) AS "Average Salary",
   100 * COUNT(*) / SUM(COUNT(*)) OVER () AS "% of employees in this range"
FROM
 (
  SELECT Salary,
    CASE  
      WHEN age >60  THEN '60 And Up'
      WHEN age >=41 and age <=60 THEN '41-60'
      WHEN age >=21 and age <=40 THEN '21-40'
      WHEN age <=20 THEN '20 And Below' 
    END as agerange
  from kin.dbo.Employee
 ) emp
group by emp.agerange 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM