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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:12:13+00:00 2026-06-15T23:12:13+00:00

I have a table with the following (simplified) structure: Order, Status, Sequence 1000, New,

  • 0

I have a table with the following (simplified) structure:

Order, Status, Sequence
1000, New, 0
1000, Viewed, 1
1000, Shipped, 2
1001, New, 0
1002, New, 0
1002, Viewed, 1
1002, New, 2

I want a SELECT statement that will return the row with the max sequence number for each order. So, in the above example, I would want the resulting dataset to be:

1000, Shipped, 2
1001, New, 0
1002, New, 2

Is there a simple way to do this? I can’t seem to construct a WHERE expression that can do it.

To respond to a few comments:

  • There are several DMBS in use where this problem arises: MS-SQL, MySQL, Oracle and Access.
  • There are so many DMBSs involved because the system being developed is consolidating data from other databases in the organization. For this reason I have no control over column names that are reserved words but I am aware of the need to escape the names.
  • Many combinations of subqueries, GROUP BY, DISTINCT, MAX and COUNT were tried to no success. Since the query results always end up in Excel I have being grabbing all the rows and sorting and removing duplicates in Excel. My preference is to skip this step by having the SQL query do the work.
  • 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-15T23:12:13+00:00Added an answer on June 15, 2026 at 11:12 pm

    SQL-pattern: finding_the_max can alway be expressed in terms of NOT EXISTS a bigger one:

    SELECT *
    FROM ztable zt
    WHERE NOT EXISTS (
        SELECT *
        FROM ztable nx
        WHERE nx.zorder = zt.zorder
        AND nx.zsequence > zt.zsequence
        );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have no control over database schema and have the following (simplified) table structure:
I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid
I have a a PostgreSQL database table with the following simplified structure: Device Id
I have a table with the following (simplified) structure: INT id, INT type, INT
I have the following table structure (simplified version) +----------------+ +-----------------+ +------+ | fee_definition |
I have the following simplified table structure in a SQL 2000 Databse: ID AppName
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have the following table (simplified): user_id date hours 1 2012-03-01 5 2 2012-03-01
I have a table with the following columns (I simplified it a little): PRODUCT_name
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,

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.