选择按month.year分组的两个日期之间的用户计数,并且对于count = 0的月份包括零

Posted

技术标签:

【中文标题】选择按month.year分组的两个日期之间的用户计数,并且对于count = 0的月份包括零【英文标题】:selecting count of user between two dates grouped by month.year and include zero for months where count=0 【发布时间】:2017-08-16 12:58:15 【问题描述】:

选择按month.year分组的两个日期之间的用户计数,并且在count=0的月份中包括零。

当前查询:

select count(u_id) as users, monthname(register_date) as month_name,year(register_date) as year
from user
where register_date >= '2017-01-01' and register_date <= '2018-01-31'
group by year(register_date), month(register_date); 

返回结果:

users, month_name, year
'1',   'August',  '2017'

预期输出:

users, month_name, year
0      'Jan'       '2017'
0      'Feb'       '2017'
0      'March'       '2017'
0      'April'       '2017'
0      'May'       '2017' 
0      'June'       '2017'
0      'July'       '2017'
1      'Aug'       '2017'
0      'Sept'       '2017'
0      'Oct'       '2017'
0      'Nov'       '2017'
0      'Dec'       '2017'
0      'Jan'       '2018'

我搜索了许多链接,但它不符合我对项目的要求。请帮助。 提前谢谢。

【问题讨论】:

mysql monthly Sale of last 12 months including months with no Sale的可能重复 【参考方案1】:

创建一个包含所有日期的表格,然后 left join 你已经拥有的。

select count(u.u_id) as users, monthname(z.Date) as 
month_name,year(z.Date) as year
from 
(
  select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) Month as Date
  from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
  cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
  cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) as z
left join user as u on z.Date = u.register_date

where z.Date >= '2017-01-01' and z.Date <= '2018-01-31'
group by year(z.Date), month(z.Date); 

Dates created like here

【讨论】:

你能再具体一点吗?年表将包括什么?年限 @Prashank 这取决于你。您预计这些年会从哪里来? 从传递的 startdate 和 enddate 开始。上面的查询在一个过程中。 所以在这种情况下,行应该是 2017 年 6 月 0 日 上述解决方案没有给出开始日期“2012-01-01”结束日期“2018-01-31”的预期结果。【参考方案2】:

请试试这个查询:

DECLARE @FromDate DATETIME, @ToDate DATETIME;
SET @FromDate = '2013-01-01';
SET @ToDate = '2014-12-31';


SELECT TOP (DATEDIFF(MONTH, @FromDate, @ToDate)+1)
 MONTH(DATEADD(MONTH, number, @FromDate)) TheMonth,
  YEAR(DATEADD(MONTH, number, @FromDate)) TheYear
  INTO Data
  FROM [master].dbo.spt_values 
  WHERE [type] = N'P' ORDER BY number;

select * from Data 

select  Data.TheMonth,Data.TheYear,  Isnull(tmp.users,0) as usercount
from    Data 
        LEFT OUTER JOIN (select count(1) as users, month(register_date) as month_name,year(register_date) as year 
                        from    user 
                        where   CONVERT(nvarchar(121), register_date , 121) >=CONVERT(nvarchar(121),@FromDate  , 121) 
                                AND CONVERT(nvarchar(121), register_date , 121) <=CONVERT(nvarchar(121), @ToDate  , 121) 
                         group by year(register_date), month(register_date) ) tmp on tmp.month_name = Data.TheMonth AND tmp.year = Data.TheYear

DROP TABLE Data

【讨论】:

MySQL 5.6 的错误【参考方案3】:

请创建一个包含所有日期的临时表(#temp_tbl),然后以下查询将产生您期望的结果:

select  count(u_id) as users, 
        monthname(register_date) as month_name,
        year(register_date) as year 
    from [user]
    where register_date >='2017-01-01' and register_date<='2018-01-31'
    group by year(register_date),month(register_date)
union
 select '0' as users, 
         monthname(register_date) as month_name,
         year(register_date) as year 
    from #temp_tbl 
    where  register_date >= '2017-01-01' and register_date<='2018-01-31'

问候, 阿卜杜拉

【讨论】:

添加代码时使用代码格式 Markdown 如何创建表?

以上是关于选择按month.year分组的两个日期之间的用户计数,并且对于count = 0的月份包括零的主要内容,如果未能解决你的问题,请参考以下文章

不在 GROUP BY 中,包含 DAY、MONTH、YEAR [重复]

两个日期之间按小时分组的秒数

MySQL按天计数和分组

如何计算 SQL Server 中按日期和用户分组的条目之间的平均时间?

Pyspark - 按两个日期列分组的 UDAF 函数,UDAF 用于计算实际值和预测值之间的 RMSE

SQL是计算两个日期相差多少天数的函数?