SQL - 另一个Select语句的子句?

时间:2010-06-25 14:10:17

标签: sql sql-server tsql sql-server-2008

我有以下查询:

select r.people_code_id [People Code ID], r.resident_commuter [Campus6],
c1.udormcom [AG], aR.RESIDENT_COMMUTER [AG Bridge], ar.ACADEMIC_SESSION,
ar.ACADEMIC_TERM, ar.academic_year, ar.revision_date
from RESIDENCY r
left join AG_Common..CONTACT1 c1 on r.PEOPLE_CODE_ID=c1.key4
left join AG_Common..CONTACT2 c2 on c1.ACCOUNTNO=c2.accountno
left join AGPCBridge..ArchiveRESIDENCY aR on r.PEOPLE_CODE_ID=aR.PEOPLE_CODE_ID
where r.ACADEMIC_YEAR='2010'
and r.ACADEMIC_TERM='Fall' 
and SUBSTRING(c1.udormcom,1,1)<>r.resident_commuter
and r.ACADEMIC_SESSION='Und 01'
and aR.ACADEMIC_SESSION='Und 01'
and aR.ACADEMIC_TERM='Fall'
and aR.ACADEMIC_YEAR='2010'
and SUBSTRING(c1.udormcom,1,1)=aR.RESIDENT_COMMUTER

我需要在where段中添加另一个子句。我有这个问题:

 select DISTINCT * from RESIDENCY where ACADEMIC_YEAR='2010' and
 ACADEMIC_TERM='Fall' and ACADEMIC_SESSION='Und 01' ORDER BY revision_date DESC

这只获得每个人的最新行。我想做(伪代码):

WHERE r.people_code_id and r.revision_date are in (select DISTINCT * from
RESIDENCY where ACADEMIC_YEAR='2010' and ACADEMIC_TERM='Fall' and
ACADEMIC_SESSION='Und 01' ORDER BY revision_date DESC)

我在SQL 2000兼容模式下运行(尽管它实际上运行的是SQL 2008)。

3 个答案:

答案 0 :(得分:2)

我根据您要添加的内容重新编写了您的查询:

WITH residency_cte AS (
     SELECT TOP (1)
            r.people_code_id, 
            r.resident_commuter,
            r.academic_year,
            r.academic_term,
            r.academic_session
       FROM RESIDENCY r
      WHERE r.academic_year = '2010'
        AND r.academic_term = 'Fall' 
        AND r.academic_session = 'Und 01'
   ORDER BY revision_date DESC)
   SELECT r.people_code_id, 
          r.resident_commuter [Campus6],
          c1.udormcom [AG], 
          aR.RESIDENT_COMMUTER,
          ar.ACADEMIC_SESSION,
          ar.ACADEMIC_TERM, 
          ar.academic_year, 
          ar.revision_date
     FROM residency_cte r
LEFT JOIN AG_Common..CONTACT1 c1 ON c1.key4 = r.PEOPLE_CODE_ID
                                AND SUBSTRING(c1.udormcom, 1, 1) != r.resident_commuter
LEFT JOIN AG_Common..CONTACT2 c2 ON c2.accountno = c1.ACCOUNTNO
LEFT JOIN AGPCBridge..ArchiveRESIDENCY aR ON aR.PEOPLE_CODE_ID = r.PEOPLE_CODE_ID
                                         AND aR.ACADEMIC_SESSION = r.academic_session
                                         AND aR.ACADEMIC_TERM = r.academic_term
                                         AND aR.ACADEMIC_YEAR = r.academic_year
                                         AND SUBSTRING(c1.udormcom, 1, 1) = aR.RESIDENT_COMMUTER

唯一的事情是udormcom列位置 - 一旦我知道它来自哪个表,我就会将该子句移动到连接中。我还将联接更新到ArchiveRESIDENCY表,因此您只需要在一个地方调整日期。

但请注意,使用子字符串匹配另一列将永远不会很好 - 直到数据模型更改为正确,这将永远不会真正优化。

答案 1 :(得分:1)

您可以使用带有子查询的EXISTS

select 
    r.people_code_id [People Code ID], 
    r.resident_commuter [Campus6],
    udormcom [AG], 
    aR.RESIDENT_COMMUTER [AG Bridge], 
    ar.ACADEMIC_SESSION,
    ar.ACADEMIC_TERM, 
    ar.academic_year, 
    ar.revision_date
from RESIDENCY r
    left join AG_Common..CONTACT1 c1 
        on r.PEOPLE_CODE_ID=c1.key4
    left join AG_Common..CONTACT2 c2 
        on c1.ACCOUNTNO=c2.accountno
    left join AGPCBridge..ArchiveRESIDENCY aR 
        on r.PEOPLE_CODE_ID=aR.PEOPLE_CODE_ID
where r.ACADEMIC_YEAR='2010'
    and r.ACADEMIC_TERM='Fall' 
    and SUBSTRING(udormcom,1,1)<>r.resident_commuter
    and r.ACADEMIC_SESSION='Und 01'
    and aR.ACADEMIC_SESSION='Und 01'
    and aR.ACADEMIC_TERM='Fall'
    and aR.ACADEMIC_YEAR='2010'
    and SUBSTRING(udormcom,1,1)=aR.RESIDENT_COMMUTER
    and EXISTS 
    (
        select 1
    FROM RESIDENCY r2
    where 1=1
        and r2.revision_date  = ar.revision_date  /* note the join here */
            and ACADEMIC_YEAR='2010' 
            and ACADEMIC_TERM='Fall' 
        and ACADEMIC_SESSION='Und 01'

        /* the order by has been removed */
    )

答案 2 :(得分:0)

WHERE r.people_code_id in (select DISTINCT people_code_id
                           from RESIDENCY 
                           where ACADEMIC_YEAR='2010' 
                             and ACADEMIC_TERM='Fall'
                             and ACADEMIC_SESSION='Und 01' 
                             and revision_date = r.revision_date
                           ORDER BY revision_date DESC)

我认为你不需要'distinct'或'order by'。删除它们可以提高性能。