以正确的顺序获取行号结果?

Get row number result in correct order?

我有一个查询要根据 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 顺序。

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

Partition by c.id ORDER BY Iscurrentjob DESC, yearleftpost DESC

这应该可以完成工作。

 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

有条件排序:

ROW_NUMBER() OVER (
  Partition by c.id 
  ORDER BY CASE WHEN cfe.Iscurrentjob = 1 THEN 0 ELSE 1 END, yearleftpost DESC
) AS JobRanking