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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:31:00+00:00 2026-06-11T09:31:00+00:00

I have a table with many rows of data, which has an column filled

  • 0

I have a table with many rows of data, which has an column filled with a date.
I need to select their first row and their last row between two dates.

Example:
Score table:

id      userId       value1         value2        date
1       1            1              2             2012-09-01 00:00:00
2       1            5              8             2012-09-03 00:00:00
3       1            7              11            2012-09-04 00:00:00
4       1            9              14            2012-09-05 00:00:00

I need to get the values value1 and value2 between the dates 2012-09-02 00:00:00 and 2012-09-04 00:00:00 of userId 1.

The desired result should be:

userId    value1_old    value2_old    value1_new    value2_new
1         5             8             7             11

The values _old should contain the data older then, but closest to the date 2012-09-02 00:00:00.
The values _new should contain the data earlier then, but closest to the date 2012-09-04 00:00:00

The problem is, how do I get these data? I don’t know how to get this to work.

Thanks in advance,
Mark

  • 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-11T09:31:01+00:00Added an answer on June 11, 2026 at 9:31 am
    SELECT   t.userId,
             first.value1 AS value1_old,
             first.value2 AS value2_old,
              last.value1 AS value1_new,
              last.value2 AS value2_new
    FROM     (
      SELECT   userId, MIN(date) AS first, MAX(date) AS last
      FROM     Score
      WHERE    userId = 1
           AND date BETWEEN '2012-09-02 00:00:00' AND '2012-09-04 00:00:00'
    ) t
        JOIN Score first ON first.userId = t.userId AND first.date = t.first
        JOIN Score  last ON  last.userId = t.userId AND  last.date = t.last
    

    See it on sqlfiddle.

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

Sidebar

Related Questions

I have a table with many rows and each row has select lists. Something
I have a table called 'customer' which has too many rows & the decision
I have many rows of data. Each row has 3 cells - id, eesnimi,
I have a table which holds the data of many seq_id. Each seq_id has
I have a table with multiple rows, each row has: layout (select) images *
I have a table with many rows of data, and I want to show
I have a data table with many rows and columns. How can I display
I have a table, has many rows. for example one of from rows(all rows
I have a table, has many rows. for example one of from rows(all rows
I have a html table which has several rows - lets say 17 for

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.