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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:37:32+00:00 2026-05-13T12:37:32+00:00

Table structure – Data present for 5 min. slots – data_point | point_date 12

  • 0

Table structure – Data present for 5 min. slots –

data_point | point_date

12 | 00:00

14 | 00:05

23 | 00:10

10 | 00:15

43 | 00:25

10 | 00:40

When I run the query for say 30 mins. and if data is present I’ll get 6 rows (one row for each 5 min. stamp). Simple Query –

select data_point
from some_table
where point_date >= start_date
AND point_date < end_date
order by point_date

Now when I don’t have an entry for a particular time slot (e.g. time slot 00:20 is missing), I want the “data_point” to be returned as 0

The REPLACE, IF, IFNULL, ISNULL don’t work when there no rows returned.

I thought Union with a default value would work, but it failed too or maybe I didn’t use it correctly.

Is there a way to get this done via sql only ?

Note : Python 2.6 & mysql version 5.1

  • 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-13T12:37:32+00:00Added an answer on May 13, 2026 at 12:37 pm

    Yes, you can do that using SQL only. A solution would be to use a Stored Routine. The bellow Stored Procedure produces following output:

    start   cnt
    00:05:00   1
    00:10:00   0
    00:15:00   1
    00:20:00   0
    00:25:00   1
    00:30:00   0
    00:35:00   1
    00:40:00   0
    00:45:00   0
    00:50:00   0
    00:55:00   2
    

    The table I used:

    CREATE TABLE `timedata` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `c1` datetime DEFAULT NULL,
      `c2` varchar(20) DEFAULT NULL,
      PRIMARY KEY (`id`)
    )
    

    Here the Stored Procedure (adjust for your environment):

    DROP PROCEDURE IF EXISTS per5min;
    DELIMITER //
    CREATE PROCEDURE per5min ()
    BEGIN
      DECLARE dtMin DATETIME;
      DECLARE dtMax DATETIME;
      DECLARE dtStart DATETIME;
      DECLARE dtStop DATETIME;
      DECLARE tmDiff TIME;
      DECLARE result INT UNSIGNED;
      SET @offset = 5 * 60;
      SELECT MIN(c1) into dtMin FROM timedata;
      SELECT MAX(c1) into dtMax FROM timedata;
    
      CREATE TEMPORARY TABLE tmp_per5min (
          start TIME,
          cnt INT UNSIGNED
      );
    
      SET dtStart = dtMin;
      REPEAT
        SELECT dtStart + INTERVAL @offset SECOND into dtStop;
        SELECT count(c2) into result FROM timedata WHERE c1 BETWEEN dtStart and dtStop;
        SELECT TIME(SUBTIME(dtStop,TIME(dtMin))) into tmDiff;
        INSERT INTO tmp_per5min (start,cnt) VALUES (tmDiff,result);
        SET dtStart = dtStop;
      UNTIL dtStop >= dtMax END REPEAT;
    
      SELECT * FROM tmp_per5min;
      DROP TABLE tmp_per5min;
    END;
    //
    DELIMITER ;
    
    CALL per5min();
    

    If you save the above into a file called ‘per5minproc.sql’, you can load it like this:

    shell> mysql -uroot test < per5minproc.sql
    

    In Python using MySQLdb (I didn’t get this working in MySQL Connector/Python, me ashamed!):

    import MySQLdb as m
    
    if __name__ == '__main__':
        db = m.connect(user='root',db='test')
        c = db.cursor()
        c.callproc("per5min")
        print(c.fetchall())
        c.close()
        db.close()
    

    The solution above works, but probably will need some tweaking, e.g. dtStart can be an argument to the SP.
    And, it’s indeed all SQL!

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

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can loop over all pairs like this: foreach( KeyValuePair<int,… May 13, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer You should bind to ActualWidth and ActualHeight members of the… May 13, 2026 at 10:49 pm
  • Editorial Team
    Editorial Team added an answer Are you looking for an installer you can use the… May 13, 2026 at 10:49 pm

Related Questions

table structure id | message | reply_id 1 | help me! | 0 434
Table structure- int PrimaryKey DateTime Date int Price What I would like to do
My table structure looks like this: tbl.users tbl.issues +--------+-----------+ +---------+------------+-----------+ | userid | real_name
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
Quickie: Table structure: id | name | parent_id I want to run a query

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.