0

我有一个查询来获取基于 yearleftpost 的 JobRanking 以获得最新的工作,然后是以前的工作。

select
    c.id,
    c.Fname,
    cfe.JobYear,
    cfe.YearLeftPost,
    cfe.Iscurrentjob,                                         
    ROW_NUMBER() OVER (Partition by c.id ORDER BY Iscurrentjob DESC, yearleftpost DESC) AS JobRanking
from
candidates c
left join Candidate_FunctionalExperience cfe on c.id = cfe.CandidateID

它给了我结果:

id  Fname    JobYear  YearLeftPost  Iscurrentjob JobRanking
EC2 sdsadsa  1430     1433              1           1
EC2 sdsadsa  1429     1430              0           2
EC2 sdsadsa  1424     1428              0           3
EC2 sdsadsa  1428     1428              0           4
EC2 sdsadsa  1424     1426              0           5

现在,我有一个 IscurrentJob(布尔值)0 或 1,我没有得到 JobRanking 的正确顺序。

4

3 回答 3

1

如果您希望Iscurrenjjob=1行在排名中排名第一,请将其添加到您的排序子句中:

Partition by c.id ORDER BY Iscurrentjob DESC, yearleftpost DESC
于 2020-01-22T17:13:19.290 回答
0

这应该可以完成这项工作。

 select
    c.id,
    c.Fname,
    cfe.JobYear,
    cfe.YearLeftPost,
    cfe.Iscurrentjob,                                         
    ROW_NUMBER() OVER (order by c.id) AS JobRanking
 from
 candidates c
 left join Candidate_FunctionalExperience cfe on c.id = cfe.CandidateID
 ORDER BY cfe.Iscurrentjob desc, cfe.YearLeftPost desc
于 2020-01-22T17:24:02.560 回答
0

使用条件排序:

ROW_NUMBER() OVER (
  Partition by c.id 
  ORDER BY CASE WHEN cfe.Iscurrentjob = 1 THEN 0 ELSE 1 END, yearleftpost DESC
) AS JobRanking
于 2020-01-22T17:25:20.590 回答