简体   繁体   English

获取所有月份的列表和当月订购的产品数量,没有订单的月份应该为“0” - mysql

[英]Getting the list of all months and the number of products ordered for the month, and should have '0'for the month with no orders - mysql

I have a set of data that looks like this我有一组看起来像这样的数据

Cust Id order date  Ordered Product
1         Jan 2           1
1         Jan 5           2
1         March 14        1      
1        September 9      1
1        December 12      2
2          Jan 5          1
2          Feb 13         2
3         March 12        2
3          April 5        3
3          June 10        2

and my output should look like this我的 output 应该是这样的

Cust Id                     order Date                  Order product
  1                           Jan 31                           3
  1                            feb 29                          0
  1                            Mar31                           1
  1                           Apr 30                           0
  1                           May 31                           0
  1                           June 30                          0
  1                           July 31                          0
  1                           Aug 31                           0
  1                           Sept 30                          1
  1                           oct 31                           0
  1                           Nov 30                           0
  1                           Dec 31                           2

and I have got this far我已经走到这一步了

1   January 31       3
1   March 31         1
1   September 30     1
1   December 31      2

and my code is我的代码是

select customer_id, 
date_format(last_day(order_date), '%M %d') as new_months, 
sum(products_ordered) as total
from amazon_test
where customer_id =1
group by new_months, customer_id;

I currently stuck at the part where I need to have all the months and '0' as the output since no orders were made.我目前停留在我需要所有月份和“0”作为 output 的部分,因为没有订单。

If you are running MySQL 8.0, one option is to use a recursive query to generate the months, and then bring the table with a left join :如果您正在运行 MySQL 8.0,一种选择是使用递归查询来生成月份,然后将表与left join一起使用:

with recursive months as (
    select customer_id, date_format(min(order_date), '%Y-%m-01') order_date, max(order_date) max_order_date 
    from amazon_test
    group by customer_id
    union all
    select customer_id, order_date + interval 1 month, max_order_date 
    from months
    where order_date  + interval 1 month < max_order_date
)
select 
    m.customer_id, 
    date_format(last_day(m.order_date), '%M %d') new_months,
    coalesce(sum(t.products_ordered), 0) ordered_products
from months m
left join amazon_test t 
    on  t.customer_id = m.customer_id
    and t.order_date >= m.order_date
    and t.order_date <  m.order_date + interval 1 month
where m.customer_id = 1
group by m.customer_id, m.order_date
order by m.customer_id, m.order_date

I phrased the query so it actually operates on all customer_id s - if you remove the where clause in the outer query, you do get the results for all customers.我对查询进行了措辞,因此它实际上对所有customer_id进行操作 - 如果您删除外部查询中的where子句,您会得到所有客户的结果。 If you really want the results for only one customer, you can optimize the query by pushing the where filter to the anchor of the recusive query.如果您真的只想要一个客户的结果,您可以通过将where过滤器推送到递归查询的锚点来优化查询。

Demo on DB Fiddle : DB Fiddle 演示

customer_id | new_months   | ordered_products
----------: | :----------- | ---------------:
          1 | January 31   |                3
          1 | February 29  |                0
          1 | March 31     |                1
          1 | April 30     |                0
          1 | May 31       |                0
          1 | June 30      |                0
          1 | July 31      |                0
          1 | August 31    |                0
          1 | September 30 |                1
          1 | October 31   |                0
          1 | November 30  |                0
          1 | December 31  |                2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM