Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql Select语句,但仅返回另一个联接表的最新项_Sql_Sql Server_Sql Server 2014 - Fatal编程技术网

Sql Select语句,但仅返回另一个联接表的最新项

Sql Select语句,但仅返回另一个联接表的最新项,sql,sql-server,sql-server-2014,Sql,Sql Server,Sql Server 2014,比如说,我选择了一位候选人,详细介绍了他们的工作经历 Select candidate.id, candidate.firstname +' '+ candidate.lastname as 'Candidate Name', cwh.dateworked from candidate inner join canworkhistory cwh on candidate.id = cwh.candidateid 现在有多条关于候选人工作日期的记录,但我只想要最近的一条 如何对这些子项进行排序,

比如说,我选择了一位候选人,详细介绍了他们的工作经历

Select candidate.id, candidate.firstname +' '+ candidate.lastname as 'Candidate Name', cwh.dateworked
from candidate
inner join canworkhistory cwh on candidate.id = cwh.candidateid
现在有多条关于候选人工作日期的记录,但我只想要最近的一条


如何对这些子项进行排序,以便在上述查询中返回前1名?

您可以尝试使用窗口函数-row_number()


您可以将派生表与
分组依据
最大值
一起使用:

Select candidate.id, 
       candidate.firstname +' '+ candidate.lastname as 'Candidate Name', 
       cwh.dateworked
from candidate
inner join (
    select candidateid, max(dateworked) as dateworked
    from canworkhistory
    group by candidateid
) cwh on candidate.id = cwh.candidateid

您可以应用最新的行:

Select candidate.id, candidate.firstname +' '+ candidate.lastname as 'Candidate Name', cwh.dateworked
outer apply (select top 1 * from canworkhistory where candidate.id = canworkhistory.candidateid order by dateworked desc) cwh
from candidate
你可以试试下面的方法

 with cte as
   (
    Select candidate.id, candidate.firstname +' '+ candidate.lastname 
    as 'Candidate   Name', cwh.dateworked
    from candidate
    inner join canworkhistory cwh on candidate.id = cwh.candidateid
  ) select t.* from cte t where t.dateworked =
                ( select max(dateworked) from canworkhistory t1
                   where t1.candidateid=t.id
                     group by candidateid
                   )

您也可以尝试使用CTE

;WITH RecentWork
     AS (SELECT
           candidateid
           ,max(dateworked)
         FROM
           canworkhistory
         GROUP  BY
          candidateid)
SELECT
  candidate.id
  ,candidate.firstname + ' ' + candidate.lastname AS 'Candidate Name'
  ,RecentWork.dateworked
FROM
  candidate
  INNER JOIN RecentWork
          ON candidate.id = RecentWork.candidateid 

No FROM子句?是否有原因不能按日期排序然后返回第一行?您的
FROM
位于
内部联接之后?这只是一个说明问题要点的示例…如果能看到所有答案之间的性能比较(因为它们似乎都提供了正确的结果),那将非常有趣…也许是从其他答案中找到的最好的极简主义方法!
;WITH RecentWork
     AS (SELECT
           candidateid
           ,max(dateworked)
         FROM
           canworkhistory
         GROUP  BY
          candidateid)
SELECT
  candidate.id
  ,candidate.firstname + ' ' + candidate.lastname AS 'Candidate Name'
  ,RecentWork.dateworked
FROM
  candidate
  INNER JOIN RecentWork
          ON candidate.id = RecentWork.candidateid