当月总销售额

时间:2018-02-13 11:10:28

标签: php mysql sql

我有以下sql:

SELECT CONCAT('', DAY(OrderUpdated), '') AS OrderDate, SUM(OrderTotal) AS ChartOrderTotal
FROM Orders WHERE OrderUpdated >= '2018-1-1' AND OrderUpdated <= '2018-2-1' 
GROUP BY CONCAT(LEFT(MONTHNAME(OrderUpdated), 3), ' ', DAY(OrderUpdated), ',', YEAR(OrderUpdated)), DAYOFWEEK(OrderUpdated) 
ORDER BY DAY(OrderUpdated) ASC

以上SQL显示结果为:

2, 20000
5, 2000
7, 8200
15, 1300
20, 1200

现在,如果我希望所有缺失的日子也将结果归零。 为:

1, 0
2, 20000
3, 0
4, 0
....
20,1200
21, 0
22, 0
....

同样在我使用手动输入日期的地方。 如何在WHERE CLAUSE中选择当前月份。

仅供参考:OrderUpdated日期为CURRENT_TIMESTAMP

4 个答案:

答案 0 :(得分:0)

您还应按ID进行分组,以便获得所有值:

SELECT CONCAT('', DAY(OrderUpdated), '') AS OrderDate, SUM(OrderTotal) AS ChartOrderTotal
FROM Orders WHERE OrderUpdated >= '2018-1-1' AND OrderUpdated <= '2018-2-1' 
GROUP BY CONCAT(LEFT(MONTHNAME(OrderUpdated), 3), ' ', DAY(OrderUpdated), ',', YEAR(OrderUpdated)), DAYOFWEEK(OrderUpdated), ID
ORDER BY DAY(OrderUpdated) ASC

答案 1 :(得分:0)

请尝试以下代码:

SELECT
  DAY(OrderUpdated)                       AS OrderDate,
  if(SUM(OrderTotal), SUM(OrderTotal), 0) AS ChartOrderTotal
FROM Orders
WHERE OrderUpdated >= LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - 
  INTERVAL 2 MONTH
  AND OrderUpdated < LAST_DAY(CURRENT_DATE) + INTERVAL 1 DAY - 
  INTERVAL 1 MONTH
GROUP BY DAY(OrderUpdated)
ORDER BY DAY(OrderUpdated) ASC;

答案 2 :(得分:0)

首先,这似乎是一种编写查询的简单方法:

SELECT DAY(o.OrderUpdated) AS OrderDate, SUM(o.OrderTotal) AS ChartOrderTotal
FROM Orders o
WHERE o.OrderUpdated >= '2018-01-01' AND o.OrderUpdated < '2018-02-01' 
GROUP BY DAY(o.OrderUpdated)
ORDER BY DAY(OrderUpdated) ASC;

要完成您想要的任务,您需要LEFT JOINCalendar表或Numbers表确实会有所帮助。例如:

SELECT DAY(c.date) AS OrderDate, COALESCE(SUM(o.OrderTotal), 0) AS ChartOrderTotal
FROM Calendar c LEFT JOIN
     Orders o
     ON date(o.OrderUpdated) = c.date
WHERE c.date >= '2018-01-01' AND c.date < '2018-02-01' 
GROUP BY DAY(c.date)
ORDER BY DAY(c.date) ASC;

如果没有这样的表,您可以创建一个表:

SELECT DAY(o.OrderDate) AS OrderDate, COALESCE(SUM(o.OrderTotal), 0) AS ChartOrderTotal
FROM (SELECT 1 as dom UNION ALL SELECT 2 UNION ALL
      . . .
      SELECT 31
     ) n LEFT JOIN
     Orders o
     ON day(o.OrderUpdated) = n.dom
WHERE o.OrderDate >= '2018-01-01' AND o.OrderDate < '2018-02-01' 
GROUP BY DAY(o.OrderDate)
ORDER BY DAY(o.OrderDate) ASC;

答案 3 :(得分:0)

步骤1:将您的值放入数组中,将天数作为索引
第2步:查找当月的天数
第3步:制作一个包含所有日期和零值的数组 第4步:使用+加号

添加两个数组
$date = new DateTime('last day of this month');
$month_length = $date->format('d');
$array1 = array(
2    => 20000,
5  => 2000,
7  => 8200,
15 => 1300,
20 => 1200,
);// you have to construct this array from your result
$array2 = array_fill(1, $month_length, 0);
$result = $array1+$array2;// addition with + order is important
ksort($result);//optional
print_r($result);

输出:

Array ( [1] => 0 [2] => 20000 [3] => 0 [4] => 0 [5] => 2000 [6] => 0 [7] => 8200 [8] => 0 [9] => 0 [10] => 0 [11] => 0 [12] => 0 [13] => 0 [14] => 0 [15] => 1300 [16] => 0 [17] => 0 [18] => 0 [19] => 0 [20] => 1200 [21] => 0 [22] => 0 [23] => 0 [24] => 0 [25] => 0 [26] => 0 [27] => 0 [28] => 0 )