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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:00:20+00:00 2026-05-23T03:00:20+00:00

I have a need to return a specific number of rows from a query

  • 0

I have a need to return a specific number of rows from a query within a given start and stop time at a dynamically calculated step interval.

I’ve kept it simple here with a table consisting of a unix timestamp and a corresponding integer value.

In my example, I need to have 200 rows returned with an INCLUSIVE start time of 1307455099 and and an INCLUSIVE end time of 1307462455.

Here’s the current query I’ve developed so far. It uses the modulus of the total rows to calculate the step interval:

SELECT timestamp, value FROM soh_data
WHERE timestamp % (CAST((1307462455 - 1307455099)/200 AS SIGNED INTEGER)) = 0
AND timestamp BETWEEN 1307455099 AND 1307462455 
ORDER BY timestamp;

The first problem is that because I’m using a modulus, the start and end times aren’t always inclusive (that’s solvable with an extra query… I’m fine with that).

The second, and more difficult issue to tackle, is that the total rows returned in this case is only 196. In most queries, it’s n-1.

FYI, this is on a MySQL database with millions of rows of data.

Any insights?

  • 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-23T03:00:20+00:00Added an answer on May 23, 2026 at 3:00 am

    Since I’m fine with throwing away a few rows, but I’m not alright with too little data, I’ve come up with two different approaches.

    First: I’ve decided to adapt my query to use FLOOR instead of CAST. In my example, the quotient of the division was 21.805. SQL rounded that up to 22. The right step interval for gathering more than 200 results was 21 (yielding 205 results). Using FLOOR will give me the step number of 21 I need. Unfortunately, I haven’t fully tested this to ensure consistent results across larger sets:

    SELECT DISTINCT timestamp FROM soh_data 
    WHERE timestamp % (FLOOR((1307459460 - 1307455099)/200)) = 0 
    AND timestamp BETWEEN 1307455099 AND 1307459460 
    ORDER BY timestamp;
    

    The more reliable solution is to pre-calculate the step in code. This way, I can zero in on the step programmatically. In the following example, I use Ruby for readability, but my ultimate solution will be coded in C++:

    lower = 1307455099
    upper = 1307459460
    
    limit = 200
    range = lower..upper
    matches = 0
    stepFactor = ((upper-1) - (lower+1))/limit
    
    while (matches <= (limit - 2)) do
        matches = 0
    
        range.each { |ts| matches += 1 if (ts % stepFactor == 0) }
    
        stepFactor -= 1 # For the next attempt
    
        puts "Step factor = #{stepFactor+1}"
        puts "Matches = #{matches}"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have need to select a number of 'master' rows from a table, also
I have a need to close a parent form from within child form from
I have a need to verify a specific hyperlink exists on a given web
I have a bit of PHP code which I need to return an even
in delphi7 i have a function that i need to return a array as
$('input[type=checkbox]').unbind().click(function(e){ $(this).attr('checked', true) return false; }); I NEED to return false because I have
A continuation from this question I need a SQL statement that returns the number
I need to return only 1 row per specific database entry. For e.g. if
I have a very specific application where I need an auto-increment variable with persistent
I need help constructing a linq query that will return a list of usernames

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.