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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:39:23+00:00 2026-05-26T14:39:23+00:00

I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid

  • 0

I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid:

tick refid value
----------------
1    1     11
1    2     22
1    3     33
2    1     1111
2    3     3333
3    3     333333

Note the “missing” rows for refid 1 (tick 3) and refid 2 (ticks 2 and 3)

If possible, how can I make a query to add these missing rows using the most recent prior value for that refid? “Most recent” means the value for the row with the same refid as the missing row and largest tick such that the tick is less than the tick for the missing row. e.g.

tick refid value
----------------
1    1     11
1    2     22
1    3     33
2    1     1111
2    2     22
2    3     3333
3    1     1111
3    2     22
3    3     333333

Additional conditions:

  • All refids will have values at tick=1.
  • There may be many ‘missing’ ticks for a refid in sequence, (as above for refid 2).
  • There are many refids and it’s not known which will have sparse data where.
  • There will be many ticks beyond 3, but all sequential. In the correct result, each refid will have a result for each tick.
  • Missing rows are not known in advance – this will be run on multiple databases, all with the same structure, and different “missing” rows.

I’m using MySQL and cannot change db just now. Feel free to post answer in another dialect, to help discussion, but I’ll select an answer in MySQL dialect over others.

Yes, I know this can be done in the code, which I’ve implemented. I’m just curious if it can be done with SQL.

  • 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-26T14:39:24+00:00Added an answer on May 26, 2026 at 2:39 pm

    What value should be returned when a given tick-refid combination does not exist? In this solution, I simply returned the lowest value for that given refid.

    Revision

    I’ve updated the logic to determine what value to use in the case of a null. It should be noted that I’m assuming that ticks+refid is unique in the table.

    Select Ticks.tick
        , Refs.refid
        , Case
            When Table.value Is Null 
                Then    (
                        Select T2.value
                        From Table As T2
                        Where T2.refid = Refs.refId
                            And T2.tick =  (
                                            Select Max(T1.tick)
                                            From Table As T1
                                            Where T1.tick < Ticks.tick
                                                And T1.refid = T2.refid
                                            )
                        )
            Else Table.value
            End As value
    From    (
            Select Distinct refid
            From Table
            ) As Refs
        Cross Join  (
                    Select Distinct tick
                    From Table
                    ) As Ticks
        Left Join Table
            On Table.tick = Ticks.tick
                And Table.refid = Refs.refid
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a following query (simplified): SELECT Id FROM dbo.Entity WHERE 1 = ALL
I have following (simplified) query: SELECT ResolvedBy, COUNT(*) AS Count, fiCategory, fiSubCategory, fiSymptom FROM
I have no control over database schema and have the following (simplified) table structure:
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have the following example query: source = (SELECT DISTINCT source.* FROM (SELECT *
I'm currently using the following query (simplified indeed) : SELECT ( SELECT COUNT(id) FROM
I have the following simplified table structure in a SQL 2000 Databse: ID AppName
I have following situation (simplified, of course): MyDomain.groovy: class MyDomain { MyAnotherDomain anotherDomain //
I have the following (simplified) public enum Level { Bronze, Silver, Gold } public
I have a database where I store objects. I have the following (simplified) schema

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.