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

  • Home
  • SEARCH
  • 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 8498029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:17:27+00:00 2026-06-11T00:17:27+00:00

I need to limit the row number to one unique row in SQL query.

  • 0

I need to limit the row number to one unique row in SQL query. Here’s sample data to recognize what I’m talking about:

john doe 3000 fog horn drive , ky 40444
john doe 3001 merry lane , ky 40484

I want to return the first one in the list here’s my query :

Select 
    DISTINCT p.personID, e.citizenship, 
    rtrim(i.lastname + CASE WHEN i.suffix IS NULL THEN '' ELSE ' ' + i.suffix END) + ', ' + i.firstname + (CASE WHEN i.middlename IS NULL THEN '' ELSE ' ' + i.middlename END) StuName, 
    e.grade, i.gender, p.studentNumber, e.citizenship, e.adult, i.birthdate, 
    e.disability1, e.disability2, ad.city, e.displacedHomemaker, e.homeSchooled, 
    e.localStudentNumber, e.migrant, e.modifiedDate, e.modifiedByID,
    rtrim(Staff.lastname + CASE WHEN Staff.suffix IS NULL THEN '' ELSE ' ' + Staff.suffix END) + ', ' + Staff.firstname + (CASE WHEN Staff.middlename IS NULL THEN '' ELSE ' ' + Staff.middleName END) Staffname, 
    Staff.personID Staffid, i.lastname, i.firstname, i.middlename, i.ssn, 
    ad.phone, ad.state, ad.zip, ad.addressLine1
FROM 
    Person p 
LEFT join 
    Enrollment e ON e.personID = p.personID And isnull(e.noshow, 0) = 0 
LEFT join 
    EnrollmentKY ky ON ky.enrollmentID = e.enrollmentID 
LEFT join 
    [Identity] i ON i.identityID = p.currentIdentityID And i.personID = p.personID 
INNER join 
    Calendar c ON c.calendarID = e.calendarID 
INNER join 
    SchoolYear sy ON sy.endYear = c.endYear AND sy.active = 1 
JOIN 
    staffMember Staff ON Staff.personID = e.modifiedByID 
--join view_students s ON  s.personID = i.personID
left join 
    v_MailingAddress ad ON ad.personID = i.personID And ad.relatedBy = 'household'
                        And ad.endDate IS NULL And isnull(ad.secondary, 0) = 0 
order by 
    i.lastname, i.firstname, i.middlename

edit: need to only pick first row in SQL code because I have a problem with people that have multiple addresses it puts two rows for them and i only need first row of data for the person that has multiple addresses.

  • 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-06-11T00:17:29+00:00Added an answer on June 11, 2026 at 12:17 am

    If the personId is distinct for each of the records and they just have a different address, then you can add a field for the row_number() and then only select the records where the row_number = 1:

    select *
    from 
    (
      Select p.personID,
        e.citizenship, 
        rtrim(i.lastname + CASE WHEN i.suffix IS NULL THEN '' ELSE ' ' + i.suffix END) + ', ' + i.firstname + (CASE WHEN i.middlename IS NULL THEN '' ELSE ' ' + i.middlename END) StuName, 
        e.grade, 
        i.gender, 
        p.studentNumber, 
        e.citizenship, 
        e.adult, 
        i.birthdate, 
        e.disability1, 
        e.disability2, 
        ad.city, 
        e.displacedHomemaker, 
        e.homeSchooled,
        e.localStudentNumber,
        e.migrant,
        e.modifiedDate,
        e.modifiedByID,
        rtrim(Staff.lastname + CASE WHEN Staff.suffix IS NULL THEN '' ELSE ' ' + Staff.suffix END) + ', ' + Staff.firstname + (CASE WHEN Staff.middlename IS NULL THEN '' ELSE ' ' + Staff.middleName END) Staffname, 
        Staff.personID Staffid,
        i.lastname, 
        i.firstname, 
        i.middlename,
        i.ssn, 
        ad.phone, 
        ad.state, 
        ad.zip, 
        ad.addressLine1,
        row_number() over(partition by p.personid order by p.personid) rn -- add this field
      FROM Person p 
      LEFT join Enrollment e 
        ON e.personID = p.personID 
        And isnull(e.noshow,0)=0 
      LEFT join EnrollmentKY ky 
        ON ky.enrollmentID = e.enrollmentID 
      LEFT join [Identity] i 
        ON i.identityID = p.currentIdentityID 
        And i.personID = p.personID 
      INNER join Calendar c 
        ON c.calendarID = e.calendarID 
      INNER join SchoolYear sy 
        ON sy.endYear = c.endYear 
        AND sy.active = 1 
      JOIN staffMember Staff 
        ON Staff.personID = e.modifiedByID 
      --join view_students s ON  s.personID = i.personID
      left join v_MailingAddress ad 
        ON ad.personID = i.personID 
        And ad.relatedBy = 'household'
        And ad.endDate IS NULL 
        And isnull(ad.secondary,0)=0 
    ) x
    where x.rn = 1
    order by x.lastname, x.firstname, x.middlename
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to limit the number of characters that can be appended to an
I have an events log database. I need to limit the size of SQL
i need to know if exist a jquery's plugin to limit the number of
I need to limit the amount of time and cpu taken by external command
I need to limit the input from a user to only positive values, and
I really need to limit any unnecessary network traffic and server trips. Solution: common
I have a PHP page I need to limit execution access of to only
I need a way to limit the meta box to a single page (ID=84)
i need a timer that works like this : imagine somebody have a limit
I'm on a strict time limit and I really need a regex to parse

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.