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 9245221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:13:04+00:00 2026-06-18T09:13:04+00:00

The database I’m accessing has two tables I need to query using DB2 SQL,

  • 0

The database I’m accessing has two tables I need to query using DB2 SQL, shown here as nametable and addresstable. The query is for finding all of the people with a certain balance due. The addresses are stored in a separate table to keep track of address changes. In addresstable, the latest address is determined by a sequence number (ADDRSEQUENCE). The AddressID field is present in both tables, and is what ties each person to specific addresses. The highest sequence number is the current address. I need that current address for each person and only that one. I know I’m going to have to use MAX somewhere for the sequence number, but I can’t figure out how to position it given the join. Here’s my current query, which of course returns all addresses…

SELECT NAMETABLE.ACCTNUM AS ACCOUNTNUMBER, 
NAMETABLE.NMELASTBUS AS LASTNAME,
NAMETABLE.NAME_FIRST AS FIRSTNAME, 
NAMETABLE.BALDUE AS BALANCEDUE,
ADDRESSTABLE.STREETNAME AS ADDR, 
ADDRESSTABLE.ADDRLINE2 AS 
ADDRLINE2,ADDRESSTABLE.CITYPARISH AS CITY, 
ADDRESSTABLE.ADDRSTATE AS STATE,
ADDRESSTABLE.ZIPCODE AS ZIP, 
ADDRESSTABLE.ADDIDSEQNO AS ADDRSEQUENCE
FROM NAMETABLE JOIN ADDRESSTABLE ON NAMETABLE.ADDRESSID = ADDRESSTABLE.ADDRESSID
WHERE NAMETABLE.BALANCEDUE >= '50.00'
  • 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-18T09:13:05+00:00Added an answer on June 18, 2026 at 9:13 am

    You can do a sub-select on the MAX(ADDRSEQUENCE) like so:

    SELECT 
         N.ACCTNUM AS ACCOUNTNUMBER
        ,N.NMELASTBUS AS LASTNAME
        ,N.NAME_FIRST AS FIRSTNAME
        ,N.BALDUE AS BALANCEDUE
        ,A.STREETNAME AS ADDR, 
        ,A.ADDRLINE2 AS 
        ,A.ADDRLINE2
        ,A.CITYPARISH AS CITY, 
        ,A.ADDRSTATE AS STATE,
        ,A.ZIPCODE AS ZIP, 
    FROM NAMETABLE AS N 
    JOIN ADDRESSTABLE AS A 
      ON N.ADDRESSID = A.ADDRESSID
    WHERE N.BALANCEDUE >= '50.00'
      AND A.ADDRSEQUENCE = (
        SELECT MAX(ADDRSEQUENCE)
        FROM ADDRESSTABLE AS A2
        WHERE A.ADDRESSID = A2.ADDRESSID
    )
    

    This is pretty quick in DB2.

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

Sidebar

Related Questions

Database has tables Photos and PhotoAlbums. I need a query that will select all
Database is OracleXE and here is the problem: data gets entered in tables UPS
Database has table X and tables An, Bn, Cn, Dn that inherits from X.
database/SQL novice here. I have a table with a column called, for example, EmployeeID.
Database Name = MyDB Table Name = MyTable Column Name = ColumnSurname Using SQL
Database one is called widgets, it has id and title. Database two is called
My database query has been running very fast until it changed to very slow
Database: SQL Server 2008 I need to produce a sales report which groups sales
Database: SQL Server 2005 I'm using a function that creates a comma separated list
My database table has a column that contains SQL timestamps (eg. 2009-05-30 19:43:41). I

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.