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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:09:38+00:00 2026-06-02T17:09:38+00:00

I have table with id, item_id, value (int), run (datetime) and i need select

  • 0

I have table with id, item_id, value (int), run (datetime) and i need select value diff betwen last two run per *item_id*.

  • 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-02T17:09:40+00:00Added an answer on June 2, 2026 at 5:09 pm
    SELECT item_id, ABS(value1 - value2) AS diff
    FROM (  SELECT h.item_id, h.value AS value1, h2.value AS value2
            FROM (  SELECT id, item_id, value
                    FROM table_name
                    GROUP BY item_id
                    ORDER BY run DESC) AS h
            INNER JOIN (    SELECT id, item_id, value
                            FROM table_name
                            ORDER BY run DESC) AS h2
            ON h.item_id = h2.item_id AND h.id != h2.id
            GROUP BY item_id) AS h3
    

    I believe this should do the trick for you. Just replace table_name to correct name.

    Explanation:
    Basicly I join the table with itself in a run DESC order, JOIN them based on item_id but also on id. Then I GROUP BY them again to remove potential 3rd and so on cases. Lastly I calculate the difference between them through ABS(value1 – value2).

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

Sidebar

Related Questions

Assuming I have the following schema table A: ID int primary key value varchar(255)
I have a db table with 3 different {price (value> int)} parameters I would
I have the following tables: create temporary table Items (item_id int, item_name varchar(10)); create
I have a table Transactions(store_id, item_id, price). I want to find store_id's which sell
I have a table that I wish to select a subset of columns from
All I have two table 1st table is : wp_frm_item_metas 2nd table is :
I have a table like this: create table t1 { person_id int, item_name varchar(30),
I currently have a table like this: Stuff ---------- StuffId identity int not null
I have the following table: ThisCategoryID int IDENTITY, AUTO_INCREMENT Title text Type text CategoryID
I have the following SQL script: DECLARE @temp table ( ID int IDENTITY(1, 1),

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.