<i id='WTtzk'><tr id='WTtzk'><dt id='WTtzk'><q id='WTtzk'><span id='WTtzk'><b id='WTtzk'><form id='WTtzk'><ins id='WTtzk'></ins><ul id='WTtzk'></ul><sub id='WTtzk'></sub></form><legend id='WTtzk'></legend><bdo id='WTtzk'><pre id='WTtzk'><center id='WTtzk'></center></pre></bdo></b><th id='WTtzk'></th></span></q></dt></tr></i><div id='WTtzk'><tfoot id='WTtzk'></tfoot><dl id='WTtzk'><fieldset id='WTtzk'></fieldset></dl></div>
<legend id='WTtzk'><style id='WTtzk'><dir id='WTtzk'><q id='WTtzk'></q></dir></style></legend>

    <small id='WTtzk'></small><noframes id='WTtzk'>

      • <bdo id='WTtzk'></bdo><ul id='WTtzk'></ul>
      <tfoot id='WTtzk'></tfoot>

    1. 将记录按小时或按天分组,并在 mysql 中用零或空值填充空白

      Grouping records hour by hour or day by day and filling gaps with zero or null in mysql(将记录按小时或按天分组,并在 mysql 中用零或空值填充空白)

      <small id='fedyD'></small><noframes id='fedyD'>

            <i id='fedyD'><tr id='fedyD'><dt id='fedyD'><q id='fedyD'><span id='fedyD'><b id='fedyD'><form id='fedyD'><ins id='fedyD'></ins><ul id='fedyD'></ul><sub id='fedyD'></sub></form><legend id='fedyD'></legend><bdo id='fedyD'><pre id='fedyD'><center id='fedyD'></center></pre></bdo></b><th id='fedyD'></th></span></q></dt></tr></i><div id='fedyD'><tfoot id='fedyD'></tfoot><dl id='fedyD'><fieldset id='fedyD'></fieldset></dl></div>
            <legend id='fedyD'><style id='fedyD'><dir id='fedyD'><q id='fedyD'></q></dir></style></legend>

              <bdo id='fedyD'></bdo><ul id='fedyD'></ul>
            • <tfoot id='fedyD'></tfoot>
                  <tbody id='fedyD'></tbody>
              1. 本文介绍了将记录按小时或按天分组,并在 mysql 中用零或空值填充空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我写了一个查询,按小时计算记录:

                I have written a query that counts records hour by hour:

                select TO_CHAR(copied_timestamp, 'YYYY-MM-DD HH24'),count(*) from req group by
                TO_CHAR(copied_timestamp, 'YYYY-MM-DD HH24');
                

                结果是:

                2012-02-22 13    2280
                2012-02-22 15    1250
                2012-02-22 16    1245
                2012-02-22 19    1258
                

                但我需要这样的结果:

                2012-02-22 13    2280
                2012-02-22 14    0
                2012-02-22 15    1250
                2012-02-22 16    1245
                2012-02-22 17    0
                2012-02-22 18    0
                2012-02-22 19    1258
                

                我也有这些按天和月分组的查询!

                Also I have these queries that group by day and month too!

                select TO_CHAR(copied_timestamp, 'YYYY-MM-DD'),count(*)  from req
                group by TO_CHAR(copied_timestamp, 'YYYY-MM-DD');
                
                select TO_CHAR(copied_timestamp, 'YYYY-MM'),count(*)  from req
                group by TO_CHAR(copied_timestamp, 'YYYY-MM');
                

                我也需要用零或空来填充它们的间隙.任何帮助都非常感谢.

                I need their gaps to be filled with zero or null too. Any help is really appreciated.

                注意:这个问题在 oracle 中有一个使用 CONNECT BY 的答案,但我需要在 Mysql 中得到答案,因为 Mysql 不支持 CONNECT BY.这是链接

                Note: There is an answer for this question in oracle using CONNECT BY but i need the answer in Mysql because Mysql does not support CONNECT BY. Here is the link

                推荐答案

                我创建了一个名为 TBL_NUMBERS 的表

                I created a table called TBL_NUMBERS

                CREATE TABLE `TBL_NUMBER` (`n` int(11) NOT NULL)
                

                并插入从 1 到 1000 的记录.现在我可以使用这个查询生成任何类型的日期范围:

                and inserted records from 1 to 1000. Now I can generate any kind of date range using this query:

                SELECT '2012-06-21' + INTERVAL n-1 [DAY | HOUR | MINUTE] or as dateRange
                 FROM TBL_NUMBER
                WHERE '2012-06-21' + INTERVAL n-1 [DAY | HOUR | MINUTE] <= '2012-06-23';
                

                然后我可以将这个表格与我的结果结合起来以填补日期空白.如果我需要超过 1000 个日期范围,我可以在 TBL_NUMBER

                Then I can join this table with my results to fill the date gap. If i need more than 1000 date range I can insert more records in TBL_NUMBER

                如果你有更好的想法,我很想知道;)

                If you have any better idea, I'm eager to know that ;)

                这篇关于将记录按小时或按天分组,并在 mysql 中用零或空值填充空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                SQL query to group by day(按天分组的 SQL 查询)
                What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)
                MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)
                MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)
                Include missing months in Group By query(在 Group By 查询中包含缺失的月份)
                Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)
                  <tbody id='4rEyi'></tbody>
                    <bdo id='4rEyi'></bdo><ul id='4rEyi'></ul>

                        <legend id='4rEyi'><style id='4rEyi'><dir id='4rEyi'><q id='4rEyi'></q></dir></style></legend>
                        <tfoot id='4rEyi'></tfoot>

                        <small id='4rEyi'></small><noframes id='4rEyi'>

                        <i id='4rEyi'><tr id='4rEyi'><dt id='4rEyi'><q id='4rEyi'><span id='4rEyi'><b id='4rEyi'><form id='4rEyi'><ins id='4rEyi'></ins><ul id='4rEyi'></ul><sub id='4rEyi'></sub></form><legend id='4rEyi'></legend><bdo id='4rEyi'><pre id='4rEyi'><center id='4rEyi'></center></pre></bdo></b><th id='4rEyi'></th></span></q></dt></tr></i><div id='4rEyi'><tfoot id='4rEyi'></tfoot><dl id='4rEyi'><fieldset id='4rEyi'></fieldset></dl></div>