SQL嵌套的选择和别名

时间:2014-10-13 22:11:48

标签: sql ms-access select alias

情况

我有一个典型的MS Access数据库,其中包含有关公司,薪酬,员工和职位的信息。一些表格是:

  1. tbl_Report(Report_ID PK,Report_Year)
  2. tbl_Employee(Employee_ID PK)
  3. tbl_Pay(Pay_ID PK,Salary,Employee_ID FK,Report_ID FK)
  4. tbl_Position(Position_ID PK,Position,Employee_ID FK,Report_ID FK)
  5. 我有一个查询,为每个职位和年份选择薪水,以生成:

    qry_Salary_by_Position_Year:   (This query is parameterised to accept a 'Year').
    
    Year | Salary | Position
    ------------------------
    2014 |  100   |   CEO
    2013 |  200   |   CEO
    2014 |  300   |   CFO
    2014 |  200   |  Chairman
    2013 |  150   |   CEO
    
    etc.
    

    然后我使用另一个查询来提取给定位置的工资的前x%:

    qry_Select_Top_25:
    
    SELECT TOP 25 PERCENT Salary, Year, Position
    FROM qry_Salary_by_Position_Year;
    

    给出了类似的东西:

    Salary | Year | Position
    ------------------------
     100   | 2014 |  CEO
     100   | 2014 |  CFO
     200   | 2014 |  CFO
    

    问题

    我想要的是一个决赛桌,显示Max(25%),Max(50%),Max(75%),Max(X%)值,按职位和年份分组,例如:

    Year | Position | 25th | 50th | 75th 
    -------------------------------------
    2013 |   CEO    |  10  |  30  |  75  
    2014 |   CEO    |  20  |  50  |  80  
    2014 |   CFO    |  15  |  30  |  90
    2014 | Chairman |  20  |  25  |  30
    

    我可以使用

    为一个百分位值执行此操作
    SELECT Year, Position, Max(qry50.Salary) AS 50_Percentile
    FROM (SELECT TOP 50 PERCENT qry_Salary_by_Position_Year.Salary, Year, Position 
    FROM qry_Salary_by_Position_Year)  AS qry50
    WHERE Position IN  (SELECT DISTINCT Position FROM qry_Salary_by_Position_Year) AND Year IN (SELECT DISTINCT Year FROM qry_Salary_by_Position_Year)
    GROUP BY Year, Position;
    

    但我无法理解如何使用正确的别名等构建查询,以将其他百分比值添加为其他列。有没有人有任何建议/意见/问题?

    修改

    我可能已经提出了一个我现在正在检查的解决方案:

    SELECT qry.Year, qry.Position, Max(qry25.Salary) AS 25_Percentile, Max(qry50.Salary) AS 50_Percentile, Max(qry75.Salary) AS 75_Percentile, Max(qry100.Salary) AS 100_Percentile
    FROM 
    ((((qry_Salary_by_Position_Year qry
    LEFT OUTER JOIN (SELECT TOP 50 PERCENT  Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry50 ON qry.Year = qry50.Year AND qry.Position = qry50.Position)
    LEFT OUTER JOIN (SELECT TOP 25 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry25 ON qry.Year = qry25.Year AND qry.Position = qry25.Position)
    LEFT OUTER JOIN (SELECT TOP 75 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry75 ON qry.Year = qry75.Year AND qry.Position = qry75.Position)
    LEFT OUTER JOIN (SELECT TOP 100 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry100 ON qry.Year = qry100.Year AND qry.Position = qry100.Position)
    GROUP BY qry.Year, qry.Position
    

1 个答案:

答案 0 :(得分:1)

我认为这就是我之后的事情:

SELECT qry.Year, qry.Position, Max(qry25.Salary) AS 25_Percentile, Max(qry50.Salary) AS 50_Percentile, Max(qry75.Salary) AS 75_Percentile, Max(qry100.Salary) AS 100_Percentile
FROM 
((((qry_Salary_by_Position_Year qry
LEFT OUTER JOIN (SELECT TOP 50 PERCENT  Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry50 ON qry.Year = qry50.Year AND qry.Position = qry50.Position)
LEFT OUTER JOIN (SELECT TOP 25 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry25 ON qry.Year = qry25.Year AND qry.Position = qry25.Position)
LEFT OUTER JOIN (SELECT TOP 75 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry75 ON qry.Year = qry75.Year AND qry.Position = qry75.Position)
LEFT OUTER JOIN (SELECT TOP 100 PERCENT Salary, Year, Position FROM qry_Salary_by_Position_Year) AS qry100 ON qry.Year = qry100.Year AND qry.Position = qry100.Position)
GROUP BY qry.Year, qry.Position

我通过对另一篇文章的回答引出了这个解决方案:https://stackoverflow.com/a/7855015/4002530