我试图根据库存代码获取每个仓库的库存金额,但返回的总金额不正确
你能帮我找错地方吗?
产品
+----------+-------------+------------+
| barkod | stokkodu | stokadi |
+----------+-------------+------------+
| 10057 | U-56544547 | Çocuk |
| 10312 | U-56548672 | Gömlek |
| 14124 | U-56144681 | Çanta |
| 18137 | U-56511551 | Cüzdan |
| 17859 | U-16511551 | Gözlük |
| 10112 | U-56544672 | Gömlek |
| 10124 | U-56544681 | Çanta |
| 10137 | U-56545551 | Cüzdan |
+----------+-------------+------------+
仓库1
+----------+-------------+------+
| barkod | stokkodu | adet |
+----------+-------------+------+
| 10111 | U-56544664 | 27 |
| 10112 | U-56544672 | 141 |
| 10124 | U-56544681 | 84 |
| 10137 | U-56545551 | 254 |
+----------+-------------+------+
仓库2
+----------+-------------+------+
| barkod | stokkodu | adet |
+----------+-------------+------+
| 10009 | Unknown | 61 |
| 10112 | U-56544672 | 131 |
| 10124 | U-56544681 | 85 |
| 10137 | U-56545551 | 254 |
+----------+-------------+------+
我试过了
SELECT
u.stokkodu,
products.stokadi,
SUM(b.adet) AS stokadet,
SUM(c.adet) AS sayimadet
FROM (
SELECT stokkodu FROM wh1
UNION ALL
SELECT stokkodu FROM wh2 WHERE onay = 1
) u
LEFT JOIN (
SELECT
stokkodu,
adet
FROM wh1
GROUP BY stokkodu
) b ON u.stokkodu = b.stokkodu
LEFT JOIN (
SELECT
stokkodu,
adet
FROM wh2
WHERE onay = 1
GROUP BY stokkodu
) c ON u.stokkodu = c.stokkodu
LEFT JOIN products ON u.stokkodu = products.stokkodu
GROUP BY u.stokkodu
wh1和wh2可能没有相同的产品
我渴望
+----------+-------------+------------+----------+-----------+
| barkod | stokkodu | stokadi | stokadet | sayimadet |
+----------+-------------+------------+----------+-----------+
| 10111 | U-56544664 | Gömlek | 27 | 0 |
| 10112 | U-56544672 | Gözlük | 141 | 131 |
| 10124 | U-56544681 | Ayakkabı | 84 | 85 |
| 10137 | U-56545551 | Çanta | 254 | 254 |
| 10009 | Unknown | Unknown | 0 | 61 |
+----------+-------------+------------+----------+-----------+
谢谢您
1条答案
按热度按时间fnatzsnv1#
我用这个查询得到了正确的值