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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:17:13+00:00 2026-05-25T19:17:13+00:00

I’m not sure how to correctly articulate the title, so please correct it if

  • 0

I’m not sure how to correctly articulate the title, so please correct it if giving the wrong idea.
Here’s an example table named table1:

id    name1    number1    name2    number2
...   ...      ...        ...      ...
341   A        12         T        10
342   C        17         A        21
343   H        15         G        3
344   C        10         A        11
345   T        15         G        16
...   ...      ...        ...      ...

Here is what I’d like to select, for n=3:

 id    name1    number1    name2    number2    number3
 ...   ...      ...        ...      ...        ...
 341   A        12         T        10         ...
 342   C        17         A        21         ...
 344   C        10         A        11         11-10+21-17+12-10
 351   D        9          A        5          5-9+11-10+21-17
 360   A        18         C        10         18-10+5-9+11-10
 503   A        21         K        16         9
 ...   ...      ...        ...      ....       based on last 3 where name1 or name2=A

As you can see number_i belongs to name_i, i=1,2
Conditions:
– name1 or name2 must be A
– number3 depends on last 3 number_i entries (current included) where name_i=A, i=1,2

  • 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-25T19:17:13+00:00Added an answer on May 25, 2026 at 7:17 pm

    Following should get you started.

    Note that there is much room for improvement but because of my lacking knowledge regarding MySQL, I have taken the safe route that should work on every DBMS.

    SQL Statement

    SELECT  id
            , name1
            , number1
            , name2
            , number2
            , r1number1 - r1number2 + r2number1 - r2number2 + r3number1 - r3number2
    FROM    ( 
              SELECT  r1.id
                      , r1.name1
                      , r1.number1
                      , r1.name2
                      , r1.number2
                      , r1number1 = CASE WHEN r1.name1 = 'A' THEN r1.number1 ELSE r1.number2 END
                      , r1number2 = CASE WHEN r1.name1 = 'A' THEN r1.number2 ELSE r1.number1 END
                      , r2number1 = CASE WHEN r2.name1 = 'A' THEN r2.number1 ELSE r2.number2 END
                      , r2number2 = CASE WHEN r2.name1 = 'A' THEN r2.number2 ELSE r2.number1 END
                      , r3number1 = CASE WHEN r3.name1 = 'A' THEN r3.number1 ELSE r3.number2 END
                      , r3number2 = CASE WHEN r3.name1 = 'A' THEN r3.number2 ELSE r3.number1 END
              FROM    (
                        SELECT  r1id = r1.id, r2id = MAX(r2.id), r3id = MAX(r3.id)
                        FROM    (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r1
                                LEFT OUTER JOIN (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r2 ON r2.id < r1.id
                                LEFT OUTER JOIN (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r3 ON r3.id < r2.id
                        GROUP BY
                                r1.id
                      ) rid
                      INNER JOIN (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r1 ON r1.id = rid.r1id
                      LEFT OUTER JOIN (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r2 ON r2.id = rid.r2id
                      LEFT OUTER JOIN (SELECT * FROM q WHERE name1 = 'A' OR name2 = 'A') r3 ON r3.id = rid.r3id
            ) r                  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

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.