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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:14:51+00:00 2026-05-27T15:14:51+00:00

I have a table: ID int category int quantity int timestamp timestamp I want

  • 0

I have a table:

ID int
category int
quantity int
timestamp timestamp

I want to SELECT id=”id#” and return a ‘position’ which is the sum of quantity for each row that has an earlier timestamp in the same category.

Thanks for your pointers!

  • 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-27T15:14:52+00:00Added an answer on May 27, 2026 at 3:14 pm

    Try this:

    SELECT sum(quantity) FROM TABLE WHERE category='...' AND timestamp < some_timestamp
    

    If you want to select by id:

    SELECT sum(quantity) FROM mytable
    WHERE category IN (SELECT category FROM mytable WHERE id=some_id)
    AND timestamp <= some_timestamp
    

    UPDATE

    To use the timestamp from the row itself, you could do something like:

    SELECT sum(quantity) FROM mytable
    WHERE category IN (SELECT category FROM mytable WHERE id=some_id)
    AND timestamp <= (SELECT timestamp FROM mytable WHERE id=some_id)
    

    ..or use a self-join as in the Adam’s answer (maybe it wasn’t an overkill, after all.. :))

    UPDATE – Alternate solution

    This seems to work fine too..

    SELECT t1.id, sum(t2.quantity), t1.timestamp, t1.category
    FROM mytable t1 INNER JOIN mytable AS t2 ON t1.category=t2.category
    WHERE t1.id=some_id_here AND t2.timestamp <= t1.timestamp;
    

    UPDATE

    Changed < into <=, else the quantity for the selected item will not be counted!

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

Sidebar

Related Questions

I have a table for product category that has a hierarchical structure. Each Category_ID
I have a table that holds nested categories. I want to avoid duplicate names
I have a table which contains the item and category ids: create table SomeTable
I have two tables: CATEGORY and SUBCATEGORY Table Structure for CATEGORY category_id int(11) category_name
I have a pictures table that has the following columns: PICTURE_ID int IDENTITY(1000,1) NOT
I have 4 tables Table: Category CategoryID (int) Name (varchar) Table: Products ProductID (int)
Hi have a table named category which has two foreign keys to the table
I have a table that has an attribute category. E.g a table where I
I have a table:'Categories' which has two fields:Category_ID and Category. Data in Category_ID field
I have a table that has this schema: create table mytable (creation_date timestamp, value

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.