Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 880137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T12:02:46+00:00 2026-05-15T12:02:46+00:00

I have the following query: select r.people_code_id [People Code ID], r.resident_commuter [Campus6], c1.udormcom [AG],

  • 0

I have the following query:

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

I need to add another clause to the where segment. I have this query:

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

This gets only the latest row for each individual. I want to do something like (pseudo-code):

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)

I am running in SQL 2000 Compatibility mode (though it is actually running SQL 2008).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T12:02:47+00:00Added an answer on May 15, 2026 at 12:02 pm

    I re-wrote your query, based on what you wanted to add:

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following query: SELECT item.`ID`, item.`Name`, item.`UnitCost`, item.`Price`, discount.`rate` FROM item INNER
I have the following query: SELECT * FROM table_name WHERE (col_1 LIKE '%$keyword%' OR
I have the following query: SELECT * FROM ( `teams` ) WHERE `name` =
I have the following query SELECT e.topicShortName, d.catalogFileID, e.topicID FROM catalog_topics a LEFT JOIN
I have the following query SELECT s.name, s.surname, s.id_nr, s.student_nr, s.createdate, s.enddate, (SELECT count(*)
I have the following query: SELECT COUNT(*) FROM FirstTable ft INNER JOIN SecondTable st
I have the following query: SELECT o.id,o.name FROM object o WHERE ( o.description LIKE
I have the following query SELECT * FROM attend RIGHT OUTER JOIN noattend ON
I have the following query: SELECT DISTINCT w.name, count(*) FROM widgets AS w JOIN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.