UNION ALL LEFT JOIN и SUM возвращает неверное значение - PullRequest
0 голосов
/ 04 декабря 2018

Я пытаюсь получить инвентарные суммы каждого склада на основе инвентарного кода, но общие суммы возвращаются неправильно

Можете ли вы помочь мне найти мою ошибку?

Продукты

+----------+-------------+------------+
| 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 Ответ

0 голосов
/ 04 декабря 2018

Я получил правильные значения с этим запросом

SELECT
d.barkod, 
x.stokkodu,
d.stokadi,
IFNULL(b.stokadet,0) AS "stokadet",
IFNULL(c.adet,0) AS "sayimadet"
FROM (  
     SELECT stokkodu FROM wh1 
     UNION ALL 
     SELECT stokkodu FROM wh2 WHERE onay = 1 
) x 
LEFT JOIN ( 
     SELECT 
     stokkodu, 
     IFNULL(SUM(adet),0) as stokadet 
     FROM wh1 
     GROUP BY stokkodu 
) b ON x.stokkodu = b.stokkodu 
LEFT JOIN ( 
     SELECT 
     stokkodu, 
     IFNULL(SUM(adet),0) as sayimadet 
     FROM wh2 
     WHERE onay = 1 
     GROUP BY stokkodu  
) c ON x.stokkodu = c.StokKodu 
LEFT JOIN products d ON x.stokkodu = d.stokkodu 
GROUP BY x.stokkodu
...