有人可以告诉我为什么我在“年度明智”中获得总数“客户”的不同金额,即第一次查询和“月份明智”,即第二次查询。
select year(order_date), year(first_order_date),
count(distinct customer_mobile) as 'Customers',
count(customer_mobile) as 'Orders',
sum(order_amount)
from order_table
group by year(order_date),year(first_order_date)
vs
select year(order_date), month(order_date), year(first_order_date),
count(distinct customer_mobile) as 'Customers',
count(customer_mobile) as 'Orders',
sum(order_amount)
from order_table
group by year(order_date),month(order_date),year(first_order_date)
由于
答案 0 :(得分:0)
不同的总和,因为在第一个查询中,您已按两年分组
所以总和将是多年。
在第二个查询中,您按月添加了所有结果将按月添加,按年份和月份不同。
例如
user1 ---> 40 per year /if you group by year
和
user1---> 10 january , 10 favruary , 5 mars ,... ///---if you group by month.
答案 1 :(得分:0)
在第一个查询中,如果客户在一年内有多个订单,则只计算一次。在第二个查询中,客户每年/每月组合计算一次。
您可以尝试第二次查询:
select CONCAT(year(order_date), '-', month(order_date)) as yearmonth,
year(first_order_date),
count(distinct customer_mobile) as 'Customers',
count(customer_mobile) as 'Orders',
sum(order_amount)
from order_table
group by CONCAT(year(order_date), '-', month(order_date)),year(first_order_date)