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

The Archive Base Latest Questions

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

I have 3 entities (Orders, Items and OrderItems) with the following schema: OrderItems Orders

  • 0

I have 3 entities (Orders, Items and OrderItems) with the following schema:

                    OrderItems
    Orders      +---------------+
+-----------+   | id (PK)       |     Items
| id (PK)   |==<| order_id (FK) |   +-------+
| createdAt |   | item_id (FK)  |>==| id    |
+-----------+   | createdAt     |   | name  |
                | quantity      |   +-------+
                +---------------+

I need to keep a history of OrderItems, so that if an OrderItem’s quantity is changed we have a record of the original quantity for each successive change.

My problem is that I’d like to be able to select only the most recent items from the table for each order. For example:

First two (initial) OrderItems:
    (id: 1, order_id: 1, item_id: 1, createdAt: 2013-01-12, quantity: 10),
    (id: 2, order_id: 1, item_id: 2, createdAt: 2013-01-12, quantity: 10),

Later order items are amended to have different quantities, creating a new row:
    (id: 3, order_id: 1, item_id: 1, createdAt: 2013-01-14, quantity: 5),
    (id: 4, order_id: 1, item_id: 2, createdAt: 2013-01-14, quantity: 15),

My stab at the query to do this:

SELECT oi.* FROM OrderItems oi
WHERE oi.order_id = 1
GROUP BY oi.item_id
ORDER BY oi.createdAt DESC;

Which I’d hoped would produce this:

| id | order_id | item_id | createdAt  | quantity |
+----+----------+---------+------------+----------+
| 3  | 1        | 1       | 2013-01-14 | 5        |
| 4  | 2        | 2       | 2013-01-14 | 15       |

Actually produced this:

| id | order_id | item_id | createdAt  | quantity |
+----+----------+---------+------------+----------+
| 1  | 1        | 1       | 2013-01-12 | 10       |
| 2  | 2        | 2       | 2013-01-12 | 10       |

At the moment I think that just using the createdAt timestamp should be enough to identify the history of items, however I may move to linking to the previous item from each order item (linked list). If that makes it easier to make this query I’ll move to that.

  • 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-17T08:27:04+00:00Added an answer on June 17, 2026 at 8:27 am

    Try this instead:

    SELECT 
      oi.*
    FROM OrderItems oi
    INNER JOIN
    (
       SELECT item_id, MAX(createdAt) MaxDate
       FROM OrderItems
       WHERE order_id = 1
       GROUP BY item_id
    ) o2  ON oi.item_id = o2.item_id
         AND DATE(oi.CreatedAt) = DATE(o2.MaxDate)
    ORDER BY oi.createdAt DESC;
    

    SQL Fiddle Demo

    This will give you:

    | ID | ORDER_ID | ITEM_ID |  CREATEDAT | QUANTITY |
    ---------------------------------------------------
    |  3 |        1 |       1 | 2013-01-14 |        5 |
    |  4 |        1 |       2 | 2013-01-14 |       15 |
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have Orders, Items and OrderItems entities with n:n relationship between Orders and Items
I have the following Entities: Customers Companies Employees Orders Order-Elements Then I have another
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{
Lets assume I have the following 3 entities: Customer,Order,Product which interact in the View
I have entities similar to: ProductLine: id, name ProductLineContents: content_id, product_line_id Content: id, text,
I have the following layout: Entities: Order OrderItem DAO classes: OrderDAO OrderItemDAO So I
I have the following in linq-to-entities clientprojects = (from p in this.SAPMappingEntities.SAP_Master_Projects join c
New at linq to entities trying to figure this out. I have the following
Suppose say I have Order, Items, OrderItems tables with Order and Items having n:n
I have items and itemgroup tables in my database: CREATE TABLE [items]( [item_id] [int]

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.