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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:11:25+00:00 2026-05-16T11:11:25+00:00

Say I have the following 2 tables, CREATE TABLE t1( name VARCHAR(25) NOT NULL,

  • 0

Say I have the following 2 tables,

CREATE TABLE t1(
  name VARCHAR(25) NOT NULL,
  time INT,
  a INT
);

CREATE TABLE t2(
  name VARCHAR(25) NOT NULL,
  time INT,
  b INT
);

and Im looking to pull all the values (a) out of t1 with a given time, all the values with the previous time (say just time-1 for convenience) then for each name subtract the newer one from the older one and insert those values into t2 with the same time. The slow way of doing this would involve doing something like

SELECT name, a FROM t1 WHERE time = x;
SELECT name, a FROM t1 WHERE time = x-1;
(subtract the as for each name)
INSERT INTO t2 VALUES ....;

From my (limited) understanding of subqueries, there should hopefully be a way to do this all in 1 query. Any ideas? Thanks in advance 🙂

  • 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-16T11:11:25+00:00Added an answer on May 16, 2026 at 11:11 am

    It looks like you can use the INSERT ... SELECT syntax:

    INSERT INTO t2 (name, time, b)
    SELECT  ta.name, ta.time time, (ta.a - tb.a) b
    FROM    t1 ta 
    JOIN    t1 tb ON (tb.time = ta.time - 1 AND tb.name = ta.name);
    

    Test case:

    INSERT INTO t1 VALUES ('t1', 1, 100);
    INSERT INTO t1 VALUES ('t1', 2, 200);
    INSERT INTO t1 VALUES ('t1', 3, 500);
    INSERT INTO t1 VALUES ('t1', 4, 600);
    INSERT INTO t1 VALUES ('t1', 5, 800);
    INSERT INTO t1 VALUES ('t1', 6, 900);
    

    Result:

    SELECT * FROM t2;
    +------+------+------+
    | name | time | b    |
    +------+------+------+
    | t1   |    2 |  100 |
    | t1   |    3 |  300 |
    | t1   |    4 |  100 |
    | t1   |    5 |  200 |
    | t1   |    6 |  100 |
    +------+------+------+
    5 rows in set (0.00 sec)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say i have the following table: create table T ( ID int not null
Let's say we have the following table: CREATE TABLE T ( ID INT, String1
Lets say I have the following MySQL structure: CREATE TABLE `domains` ( `id` INT(10)
In the following one to many CREATE TABLE source(id int, name varchar(10), PRIMARY KEY(id));
Let's say I have the following tables: PartyRelationship EffectiveDatedAttributes PartyRelationship contains a varchar column
Let's say I have the following table: CustomerID ParentID Name ========== ======== ==== 1
I have created an sqlite table using the following statement: CREATE TABLE IF NOT
Let's say I have the following hypothetical data structure: create table country ( country_id
Say you have a table layout like the following: couses : id (INT), courseName
So I have the following example tables: Table A: id name type hat_size created

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.