[英]mysql group by year diffrent from group by year and month
I am trying to make some reports regarding our orders and the products sold, revenue and the total weight, but when im grouping our orders together I get different results in some of my queries that should return the same. 我正在尝试针对我们的订单,所售产品,收入和总重量做出一些报告,但是当将我们的订单分组在一起时,我在某些查询中得到的结果应该返回相同的结果。
The following is my queries: 以下是我的查询:
SELECT
MONTH(orders.date_purchased) as date,
YEAR(orders.date_purchased) as year,
count(DISTINCT orders.orders_id) AS total_orders,
categories.fields_23 as currency
FROM
orders_shipping_products
Inner Join orders ON orders.orders_id = orders_shipping_products.orders_id
Inner Join categories ON categories.fields_6 = orders.shopping_store_category_id
WHERE
orders.orders_status NOT IN (0, 1, 99)
GROUP BY
date,
year,
categories.fields_23
ORDER by
YEAR(orders.date_purchased),
MONTH(orders.date_purchased) ASC,
categories.fields_23
This returns for 2012 the following table, a total of 353: 这将返回2012年的下表,总计353:
+------+------+--------------+----------+
| date | year | total_orders | currency |
+------+------+--------------+----------+
| 11 | 2012 | 86 | EUR |
| 12 | 2012 | 267 | EUR |
+------+------+--------------+----------+
SELECT
YEAR(orders.date_purchased) as year,
count(DISTINCT orders.orders_id) AS total_orders,
categories.fields_23 as currency
FROM
orders_shipping_products
Inner Join orders ON orders.orders_id = orders_shipping_products.orders_id
Inner Join categories ON categories.fields_6 = orders.shopping_store_category_id
WHERE
orders.orders_status NOT IN (0, 1, 99)
GROUP BY
year,
categories.fields_23
ORDER by
YEAR(orders.date_purchased),
categories.fields_23
it returns for 2012 the following 它在2012年返回以下
+------+--------------+----------+
| year | total_orders | currency |
+------+--------------+----------+
| 2012 | 351 | EUR |
+------+--------------+----------+
The only thing that changes is the total_orders, total amount of products, the revenue and weight is the same. 唯一更改的是total_orders,产品总数,收入和重量相同。 I just get two more orders when checking per month.
每月检查一次,我再得到两个订单。 I also checked with a selection and grouping by QUARTER, YEAR that returns the following:
我还检查了选择和分组,按QUARTER,YEAR返回以下内容:
+---------+------+--------------+----------+
| quarter | year | total_orders | currency |
+---------+------+--------------+----------+
| 4 | 2012 | 351 | EUR |
+---------+------+--------------+----------+
This make me thinks, that i might be doing something wrong with my selections when i want to generate a report per month 这让我想,当我想每月生成报告时,我的选择可能做错了
Daniel asked me to try add WITH ROLLUP on my query, follow is what i did and got in return 丹尼尔(Daniel)让我尝试在查询中添加WITH ROLLUP,以下是我的工作并获得了回报
SELECT
MONTH(orders.date_purchased) as date,
YEAR(orders.date_purchased) as year,
count(DISTINCT orders.orders_id) AS total_orders,
categories.fields_23 as currency
FROM
orders_shipping_products
Inner Join orders ON orders.orders_id = orders_shipping_products.orders_id
Inner Join categories ON categories.fields_6 = orders.shopping_store_category_id
WHERE
orders.orders_status NOT IN (0, 1, 99)
GROUP BY
year,
date,
categories.fields_23 WITH ROLLUP
This is the returned data: 这是返回的数据:
+------+------+--------------+----------+
| date | year | total_orders | currency |
+------+------+--------------+----------+
| 11 | 2012 | 86 | EUR |
| 11 | 2012 | 86 | NULL |
| 12 | 2012 | 267 | EUR |
| 12 | 2012 | 267 | NULL |
| NULL | 2012 | 351 | NULL |
+------+------+--------------+----------+
I rechecked orders_id and it where not unique, in fact there where 2-3 the same orders id and some of them where located in different months. 我重新检查了orders_id,它不是唯一的,实际上是2-3个相同的订单ID,其中一些位于不同的月份。 Found that a combination of orders_id and another column made it up for the real unique id.
发现orders_id和另一列的组合弥补了真正的唯一ID。 adding this to my join, solved everything.
添加到我的加入,解决了一切。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.