我认为我的查询格式不正确,但我的Google技能尚未找到有效的解决方案。这是我的问题:
use movement;
select t1.StoreNumber, t1.saleDate, t1.departmentNumber, sum(t1.dollarsSold), sum(ifnull(t2.loss,0))
from movement.movement t1
left join knownLoss.producekl t2 ON t1.StoreNumber = t2.Store
where t1.StoreNumber = 3
AND (t1.departmentNumber = 10 OR t1.departmentNumber = 20 OR t1.departmentNumber = 27 OR t1.departmentNumber = 30 OR t1.departmentNumber = 40 OR t1.departmentNumber = 50 OR t1.departmentNumber = 51 OR t1.departmentNumber = 60 OR t1.departmentNumber = 70 OR t1.departmentNumber = 80 OR t1.departmentNumber = 81 OR t1.departmentNumber = 82 OR t1.departmentNumber = 90 OR t1.departmentNumber = 95 OR t1.departmentNumber = 96 OR t1.departmentNumber = 97)
AND t1.saleDate > date_sub(curdate(), interval 7 day)
group by t1.saleDate, t1.StoreNumber, t1.departmentNumber;
我的预期输出是在最后两列中给出按销售日期,商店和部门总结销售总额和销售损失分组的表格
我的预期输出应如下所示:
-------------------------------------------------------------
| Store Number | Sale Date | Dept Num | Dollars Sold | Loss |
-------------------------------------------------------------
| 1 | 2017-3-9 | 10 | 7435.26 | 0.00 |
-------------------------------------------------------------
| 1 | 2017-3-9 | 20 | 30.50 | 3.00 |
-------------------------------------------------------------
| 1 | 2017-3-10 | 10 | 2503.22 | 4.00 |
-------------------------------------------------------------
| 2 | 2017-3-11 | 10 | 5685.49 |17.50 |
-------------------------------------------------------------
| 3 | 2017-3-9 | 10 | 75.26 | 4.55 |
-------------------------------------------------------------
| 4 | 2017-3-9 | 10 | 7435.26 | 0.00 |
-------------------------------------------------------------
| 4 | 2017-3-13 | 30 | 20.45 | 1.99 |
-------------------------------------------------------------
损失列的全部为零,而不是我预期的输出损失列。
-------------------------------------------------------------
| Store Number | Sale Date | Dept Num | Dollars Sold | Loss |
-------------------------------------------------------------
| 1 | 2017-3-9 | 10 | 7435.26 | 0.00 |
-------------------------------------------------------------
| 1 | 2017-3-9 | 20 | 30.50 | 0.00 |
-------------------------------------------------------------
| 1 | 2017-3-10 | 10 | 2503.22 | 0.00 |
-------------------------------------------------------------
| 2 | 2017-3-11 | 10 | 5685.49 | 0.00 |
-------------------------------------------------------------
| 3 | 2017-3-9 | 10 | 75.26 | 0.00 |
-------------------------------------------------------------
| 4 | 2017-3-9 | 10 | 7435.26 | 0.00 |
-------------------------------------------------------------
| 4 | 2017-3-13 | 30 | 20.45 | 0.00 |
-------------------------------------------------------------
即使我单独查询t2,它也会返回结果
use knownLoss;
select t2.Store, t2.Department, t2.klDate, sum(t2.loss)
from producekl t2
group by klDate, Store, Department;
所以我的联系必须弄乱,但我无法弄明白。有什么想法吗?
答案 0 :(得分:1)
FWIW,我发现这更容易阅读...
我不熟悉curdatee()函数,因此我将其替换为其他内容......
SELECT t1.StoreNumber
, t1.saleDate
, t1.departmentNumber
, SUM(t1.dollarsSold)
, SUM(IFNULL(t2.loss,0))
FROM movement.movement t1
LEFT
JOIN knownLoss.producekl t2
ON t1.StoreNumber = t2.Store
WHERE t1.StoreNumber = 3
AND t1.departmentNumber IN(10,20,27,30,40,50,51,60,70,80,81,82,90,95,96,97)
AND t1.saleDate > DATE_SUB(CURDATE(), INTERVAL 7 DAY)
GROUP BY t1.saleDate
, t1.StoreNumber
, t1.departmentNumber;
请注意,GROUP BY中缺少saledate。 BAD。
答案 1 :(得分:0)
我终于解决了这个问题。我加入的表都没有唯一值,因此我必须使用子查询来生成唯一值。这是导致我期望的输出的最终查询。
SELECT t2.StoreNumber
, t2.departmentNumber
, sum(t2.Sales) AS netSales
, ifnull(sum(t3.loss),0) AS knownLoss
, ifnull(round(avg(t3.loss / t2.Sales),4),0) AS percentLoss
from ( SELECT t1.StoreNumber
, t1.departmentNumber
, SUM(t1.dollarsSold) AS Sales
FROM movement.movement t1
WHERE t1.StoreNumber = 1
AND t1.departmentNumber = 80
AND t1.saleDate >= date_sub(SUBDATE(curdate(), WEEKDAY(curdate())), interval 7 day)
AND t1.saleDate < SUBDATE(curdate(), WEEKDAY(curdate()))
GROUP BY t1.StoreNumber
, t1.departmentNumber) t2
left join
(select Store, Department, sum(loss) AS loss
from producekl
where Department = 80
AND Store = 1
AND klDate >= date_sub(SUBDATE(curdate(), WEEKDAY(curdate())), interval 7 day)
AND klDate < SUBDATE(curdate(), WEEKDAY(curdate()))
group by Store, Department) t3 ON t2.StoreNumber = t3.Store
GROUP BY t2.StoreNumber, t2.departmentNumber
感谢Strawberry鼓励良好的格式化,它帮助我更快地解决了我的问题。