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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:21:58+00:00 2026-06-03T09:21:58+00:00

Suppose I have a table with four columns: ID, ParentID, Timestamp, Value. The ParentID

  • 0

Suppose I have a table with four columns: ID, ParentID, Timestamp, Value. The ParentID changes the meaning of value. I need to return all the entries from this table where their ParentID = 1 and their Timestamp is within the Timestamps of the items in the table where ParentID = 2 and those items with ParentID.Value > 10.

For example, here is some data:

ID    ParentID    TimeStamp    Value
1     1           51           1
2     2           52           11
3     1           53           2
4     1           54           3
5     2           55           9
6     1           56           4
7     2           57           12
8     1           58           5
9     1           53.5         1

I need a query that returns me those rows with IDs 3, 4, 8, and 9. What would this query look like in SQL or LINQ? Is it permissible to join on yourself and would you use that approach here? I’m stuck on how to determine the timestamp ranges. I’m using Sqlite. Thanks.

Clarification: I want to filter by the most recent (judged by timestamp) row with ParentID = 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-06-03T09:21:59+00:00Added an answer on June 3, 2026 at 9:21 am

    This ugly and slow query finds per each ParentID = 1 row adequate ParentID = 2 row and checks if Value is greather than 10; if so, outputs row:

    select * 
      from table1
     where table1.parentid = 1
       and exists
           (
             select null
             -- Isolate previous ParentID = 2 row
             from
             (
               select p2.value
                 from table1 p2
                where p2.timestamp < table1.timestamp
                  and p2.parentid = 2
                  -- Only one needed
                order by p2.timestamp desc
                limit 1
             ) p2_1
             -- Make sure it has appropriate value
             where p2_1.value > 10
           )
    

    This is worth running only once in context of updating a table after a schema change. If you could change ParentID to really be a ParentID (that is, connect record hierarchically) query would become trivial:

    select Child.*
      from table1 Child
     inner join table1 Parent
        on child.ParentID = Parent.ID
     where Parent.Value > 10
    

    ParentID would, of course, had to be found prior to insert and there would be some mathematics involved if business model allows for out of order inserts. If all you expect is simple append of timestamps, this would be the simplest solution.

    There is Sql Fiddle live test of this query.

    • 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 four columns, say P_Key(int), Ref_Key(int), Key(String), Value(Integer). I want
Suppose I have a database table with columns a, b, and c. I plan
Suppose I have a table abc with columns p_id , u_id , and comments
Suppose I have a table with the following columns (a list of words): word:
Suppose I have a table with columns Name VARCHAR, Department VARCHAR, Gender VARCHAR, and
Suppose I have a table with columns userdef1, userdef2 .....userdef6 and I have another
Suppose I have a table TABLE with two columns COL_1 and COL_2 . I
Suppose I have a table with over a million entries and I want to
Suppose I have table with two columns. I can center this table by using:
Suppose I have a table like: id............value ``````````````````` A............1 A............2 A............3 B............1 and so

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.