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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:29:56+00:00 2026-05-26T02:29:56+00:00

I can get the results that I need, but I need a fast query.

  • 0

I can get the results that I need, but I need a fast query.

id      value1      fid
1000    1203        5
1001    1293        1
1002    1203        3
1003    1211        1
1004    1263        1
1005    1223        5
1006    1243        2
1007    123         5
1008    1049        2
1009    1205        3

Here are the results that I need:

fid id      value1      id2     previous_value1
1   1004    1263        1003    1211
2   1008    1049        1006    1243
3   1009    1205        1002    1203
5   1007    123         1005    1223

Any ideas?


Edit: I forgot to wrote my query here is my query:

SELECT
t1.fid,
t1.id,
t1.value1,
t2.id AS id2,
t2.value1 AS previous_value1
FROM
(
  SELECT
  MAX(id) AS id
  FROM TABLENAME
  GROUP BY fid
) t3
INNER JOIN TABLENAME t1 ON t1.id = t3.id
INNER JOIN TABLENAME t2 ON t2.id = (SELECT id FROM TABLENAME WHERE id < t1.id AND fid =     t1.fid ORDER BY id DESC LIMIT 1)

Actually I simplified it by removing extra columns.

(There is 2 more columns in WHERE statement but it doesn’t matters.)

The original query takes ~0.04seconds. If I filter the results with the other two columns in where statement, it takes ~0.001 seconds.

  • 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-26T02:29:56+00:00Added an answer on May 26, 2026 at 2:29 am

    Ok, here is a query that returns what you want (at least, what I think you want), but its anything but fast and efficient, so try it first:

    SELECT  A.*, 
            (SELECT TOP 1 id FROM YourTable WHERE fid = A.fid AND id < B.MaxId ORDER BY id DESC) Id2,
            (SELECT TOP 1 value1 FROM YourTable WHERE fid = A.fid AND id < B.MaxId ORDER BY id DESC) previous_value1
    FROM YourTable A
    INNER JOIN (SELECT fid, MAX(id) Maxid
                FROM YourTable 
                GROUP BY fid) B
    ON A.id = B.Maxid
    ORDER BY A.fid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get Google search results from inside a program? I need to
Googling has been difficult because I can't get results specific enough - wondering if
While using this code, i can't get any results... Ext.define('Teste.view.Main', { extend: 'Ext.Container', initialize:
I currently use a DataTable to get results from a database which I can
couple of quick questions 1) In the local search results - we can get
How can I get result from action? I need to show the commentID on
i search online but can't get satisfactory result i want to protect images on
In cake php is how we can get order of query result according to
i'm trying to get a results set that displays 2 columns from 2 different
I am in need of a data structure that has fast insert, fast retrieve,

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.