php—对数据表进行计数并求和,然后用默认值填充空数据

63lcw9qa  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(229)

我有一个只包含日期数据的表,我想计算数据在同一个月的次数,对于这个问题,它已经完成了,现在我得到了一个新的问题,就是缺少的月份,这是因为没有该月份的数据。现在我想添加默认值为0的空月份,即使该月份不在表中。有人能根据我的问题帮我吗?
这是我的资料

start_date  |end_date
------------------------
 2018-10-01 |2018-10-02
 2018-01-04 |2018-02-04
 2018-08-01 |2018-10-01

这是我的问题

select month(month_table) as month_table
         , sum(cstart) as cstart 
         , sum(cend) as cend 
      from 
         (
          (select `start_date` as month_table
                , 1 as cstart
                , 0 as cend 
             from newdata
          ) 
  union all 
          ( select `end_date`
                 , 0
                 , 1 
              from newdata 
           ) 
          ) dd 
      group 
         by monthname(month_table)
          , month(month_table) 
      order 
         by month(month_table)

结果是

month_table|cstart|cend
      1      |  1   |  0
      2      |  0   |  1
      8      |  1   |  0
      10     |  1   |  2

我想添加新的查询,这样我的结果

month_table|cstart|cend
      1      |  1   |  0
      2      |  0   |  1
      3      |  0   |  0
      4      |  0   |  0
      5      |  0   |  0
      6      |  0   |  0
      7      |  0   |  0
      8      |  1   |  0
      9      |  0   |  0
      10     |  1   |  2
      11     |  0   |  0
      12     |  0   |  0

这是我的小提琴http://sqlfiddle.com/#!9/c18b5f/3/0型

7hiiyaii

7hiiyaii1#

这应该是这个问题的答案:

IF EXISTS(select * FROM sys.views where name = 'monthnotexict')
drop view monthnotexict
go
    create view monthnotexict as
        select 1 as month_table,0 as cstart ,0 as cend  from newdata union 
        select 2 as month_table,0 as cstart ,0 as cend from newdata union 
        select 3 as month_table,0 as cstart,0 as cend from newdata union 
        select  4 as month_table,0 as cstart,0 as cend from newdata union 
        select  5 as month_table,0 as cstart,0 as cend from newdata union 
        select  6 as month_table,0 as cstart,0 as cend from newdata union 
        select  7 as month_table,0 as cstart,0 as cend from newdata union 
        select  8 as month_table,0 as cstart,0 as cend  from newdata union 
        select  9 as month_table,0 as cstart,0 as cend from newdata union 
        select  10 as month_table,0 as cstart,0 as cend from newdata union 
        select  11 as month_table,0 as cstart,0 as cend from newdata union 
        select  12 as month_table,0 as cstart,0 as cend from newdata

    go
     SELECT DATENAME(month, DATEADD(month,M.month_table-1, CAST('2008-01-01' AS datetime)))as month_table
             , sum(cstart) as cstart 
             , sum(cend) as cend 
          from monthnotexict left join
             (
              (select 
                    , 1 as cstart
                    , 0 as cend 
                 from newdata

              ) 
      union all 
              ( 
                     , 0
                     , 1 
                  from newdata 

               ) 
    union all
    (
    select
    month_table
                 , sum(cstart) as cstart 
                 , sum(cend) as cend  
        from monthnotexict
group by month_table

    )

              ) dd on dd.cend=M.cend or dd.cstart=M.cstart

  group by M.month_table

根据您在评论中的请求,我更改了代码(现在代码没有错误,月份号更改为月份名称)
我希望它完成!

h7wcgrx3

h7wcgrx32#

你需要一张满月的table。您可以使用 UNION ALL . 然后左键将count子查询联接到该表。

SELECT m.month month_table,
       coalesce(s.count, 0) cstart,
       coalesce(e.count, 0) cend
       FROM (SELECT 1 month
             UNION ALL
             SELECT 2 month
             UNION ALL
             SELECT 3 month
             UNION ALL
             SELECT 4 month
             UNION ALL
             SELECT 5 month
             UNION ALL
             SELECT 6 month
             UNION ALL
             SELECT 7 month
             UNION ALL
             SELECT 8 month
             UNION ALL
             SELECT 9 month
             UNION ALL
             SELECT 10 month
             UNION ALL
             SELECT 11 month
             UNION ALL
             SELECT 12 month) m
            LEFT JOIN (SELECT month(n.start_date) month,
                              count(*) count
                              FROM newdata n
                              GROUP BY month(n.start_date)) s
                      ON s.month = m.month
            LEFT JOIN (SELECT month(n.end_date) month,
                              count(*) count
                              FROM newdata n
                              GROUP BY month(n.end_date)) e
                      ON e.month = m.month
       ORDER BY m.month;

相关问题