gpt4 book ai didi

mysql - 添加到查询以获得以前结果 SQL 的总和

转载 作者:行者123 更新时间:2023-11-29 02:50:16 26 4
gpt4 key购买 nike

我目前有一个提供以下结果集的查询,我现在需要添加到此查询以提供所有销售额底部的总计。我不确定该怎么做。

当前查询:

SELECT
product,
COUNT(OrderNumber) AS CountOf
FROM
orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)

GROUP BY
product
ORDER BY CountOf DESC

当前结果集:

product| count
-----------------------
pd1 | 3
pd4 | 1
pd2 | 1

期望的结果集 =

product| count
-----------------------
pd1 | 3
pd4 | 1
pd2 | 1
Total | 5

最佳答案

也许您可以添加一个 UNION 和一个包含总金额的 SELECT。像这样:

SELECT
product,
COUNT(OrderNumber) AS CountOf
FROM
orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
GROUP BY
product
UNION
SELECT 'Total', count(OrderNumber) AS CountOf
FROM orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
ORDER BY CountOf DESC;

关于mysql - 添加到查询以获得以前结果 SQL 的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722122/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com