MySQL query data by hour, fill in 0 if there is no data

MySQL query data by hour, fill in 0 if there is no data

Demand background

A statistical interface, the front end needs to return two arrays, one is the hour count from 0 to 23, and the other is the statistical number corresponding to each hour.

The idea is to directly use group by to query the table to be counted. When the statistical number of a certain hour is 0, there will be no grouping for that hour. After thinking about it, I need to create an auxiliary table with only one column for hours, and then insert 0-23 for a total of 24 hours

CREATE TABLE hours_list (
 hour int NOT NULL PRIMARY KEY
)

Check the hour table first, then connect the table you need to check, and fill in the hours without statistics with 0. Here, because we need to query multiple tables, create_time is within each hour interval, and SOURCE_ID is equal to the statistical sum of the query conditions, so UNION ALL multiple tables.

	SELECT
			 t.HOUR,
			 sum(t.HOUR_COUNT) hourCount
			FROM
    (SELECT
     hs. HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_0002 cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR
    UNION ALL
    SELECT
     hs.HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_hs cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR
    UNION ALL
    SELECT
     hs.HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_kfyj cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR
    UNION ALL
    SELECT
     hs.HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_his_0002 cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR
    UNION ALL
    SELECT
     hs.HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_his_hs cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR
    UNION ALL
    SELECT
     hs.HOUR AS HOUR,
     COUNT(cs.RECORD_ID) AS HOUR_COUNT
    FROM
     cbc_hours_list hs
    LEFT JOIN cbc_source_his_kfyj cs ON HOUR (cs.create_time) = hs.HOUR
    AND cs.create_time > #{startTime}
    AND cs.create_time <= #{endTime}
    <#if sourceId?exists && sourceId !=''>
     AND SOURCE_ID = #{sourceId}
    </#if>
    GROUP BY
     hs. HOUR) t
   GROUP BY
    t.hour

Effect

Hours with a statistical count of 0 can also be found.

insert image description here

This is the end of this article about querying MySQL data by the hour and filling in zeros for missing data. For more information about querying MySQL data by the hour, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation of MySQL subqueries (nested queries), join tables, and combined queries
  • Detailed explanation of MySQL sql_mode query and setting
  • MySQL subqueries and grouped queries
  • MySQL grouping queries and aggregate functions
  • Summary of related functions for Mysql query JSON results
  • MySQL query sorting and paging related
  • MySql query time period method
  • Basic multi-table join query tutorial in MySQL
  • Subquery examples in MySQL
  • Detailed explanation of MySQL combined query

<<:  Enable sshd operation in docker

>>:  Detailed explanation of Vue mixin usage and option merging

Recommend

Understanding render in Vue scaffolding

In the vue scaffolding, we can see that in the ne...

How to get USB scanner data using js

This article shares the specific process of js ob...

Specific method to add foreign key constraints in mysql

The operating environment of this tutorial: Windo...

Vue implements a simple marquee effect

This article shares the specific code of Vue to a...

MySQL query example explanation through instantiated object parameters

This article will introduce how to query data in ...

js array entries() Get iteration method

Table of contents 1. Detailed syntax of entires()...

Pure CSS3 realizes the effect of div entering and exiting in order

This article mainly introduces the effect of div ...

Analysis of mysql view functions and usage examples

This article uses examples to illustrate the func...

Some improvements in MySQL 8.0.24 Release Note

Table of contents 1. Connection Management 2. Imp...

Summary of MySQL lock related knowledge

Locks in MySQL Locks are a means to resolve resou...