代码之家  ›  专栏  ›  技术社区  ›  Dave Mackey chem1st

另一个select语句的where子句?

  •  2
  • Dave Mackey chem1st  · 技术社区  · 16 年前

    我有以下问题:

    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 回复  |  直到 16 年前
        1
  •  2
  •   OMG Ponies    16 年前

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

    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
    

    Only thing is the udormcom column location - once I know what table it's from, I'd move the clause up into the joins. I also updated the joins to the ArchiveRESIDENCY table, so you only need to tweak the dates in one place.

    But be aware that using a substring to match on another column will never perform well - until the data model changes to correct that, this will never be truly optimized.

        2
  •  1
  •   Raj More    16 年前

    You could use an EXISTS with a subquery

    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 */
        )
    
        3
  •  0
  •   Kenneth    16 年前
    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)
    

    我认为你不需要“独特”或“排序依据”。移除这些应该提高性能。