很难搜索到它非常具体,我目前在使用我的查询时看到下面的数据集,问题是在net栏下这应该是每个产品的净值加起来(数据集会显示什么我的意思是),但我无法实现这一点,在工作时我还想为此添加一个thrid列并将值相加,所以重要的是要做到这一点。
当前查询:
SELECT
product, Net,
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
数据集:
id | product | status | Net
-----------------------------
1 | pd1 |quote | 10
2 | pd2 |booking | 15
3 | pd4 |booking | 20
4 | pd2 |cancelled| 15
5 | pd2 |booking | 15
6 | pd1 |booking | 10
7 | pd1 |booking | 10
当前结果集:
product | Sales | Net
------------------------
pd1 |3 | 10
pd2 |3 | 15
pd4 |1 | 20
期望的结果集:
product | Sales | Net
------------------------
pd1 |3 | 30
pd2 |3 | 45
pd4 |1 | 20
我认为这应该有意义
答案 0 :(得分:2)
对Net列使用sum。这是你的解决方案:
SELECT
product, sum(Net) as Net,
COUNT(OrderNumber) AS CountOf
FROM
sw_orders
WHERE
STATUS = 'booking' AND
Date(OrderDate) <= CURDATE() AND
Date(OrderDate) > DATE_SUB(CURDATE(),INTERVAL 30 DAY)
GROUP BY
product
答案 1 :(得分:1)
SELECT
product, SUM(Net) AS Net,
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