0

我有一个代码,我在其中提取语言知识,每个员工都有一个计划年,他们并不是每年都完成它们,为了获得他们最近的一个,我使用 MAX 作为计划年。现在的标准之一是他们是否愿意在海上移动,出现的问题是它将提出他们最近的“是”和“最近的”“否”,我只需要他们最近的计划年,无论是或否,我我很难解决这个问题。代码如下:

SELECT Employee_ID, Accept_International_Assignment, MAX(Plan_Year) AS Expr1
FROM  dbo.v_sc08_CD_Employee_Availabilities
GROUP BY Employee_ID, Accept_International_Assignment    
4

4 回答 4

4

我怀疑这将比大规模接受的答案更有效......

;WITH x AS 
(
  SELECT Employee_ID, Accept_International_Assignment, Plan_Year,
    rn = ROW_NUMBER() OVER (PARTITION BY Employee_ID ORDER BY Plan_Year DESC)
  FROM dbo.v_sc08_CD_Employee_Availabilities -- who comes up with these names?
)
SELECT Employee_ID, Accept_International_Assignment, Plan_Year
  FROM x WHERE rn = 1;
于 2013-09-04T17:50:54.217 回答
2
SELECT a.Employee_ID, a.Accept_International_Assignment, a.Plan_Year
FROM  dbo.v_sc08_CD_Employee_Availabilities a
INNER JOIN (SELECT Employee_ID, MAX(Plan_Year) maxPlanYear
            from dbo.v_sc08_CD_Employee_Availabilities
            GROUP BY Employee_ID) m
   ON a.Plan_Year = m.maxPlanYear AND a.Employee_ID = m.Employee_ID
于 2013-09-04T17:34:34.330 回答
0

我不确定您是否只想要 Raphael 发布的最新决定和年份,或者您是否想要是和否,但始终为该员工提供 Max 计划年份。

这是对“是”和“否”的查询,但 Max plan year 始终是员工的最大值。

select main.Employee_ID, Accept_International_Assignment, Expr1
from (
    SELECT Employee_ID, Accept_International_Assignment
    FROM  @v_sc08_CD_Employee_Availabilities
    GROUP BY Employee_ID, Accept_International_Assignment  
) main
inner join
(
 select Employee_ID, MAX(Plan_Year) as Expr1
 from @v_sc08_CD_Employee_Availabilities
 group by Employee_ID) empPlanYear
on main.Employee_ID = empPlanYear.Employee_ID
于 2013-09-04T17:47:18.467 回答
-2

你需要一个关于 max() 的子查询并加入它。

SQL Group by & Max

于 2013-09-04T17:34:35.523 回答