union all left join and sum返回不正确的值

sy5wg1nm  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(492)

我试图根据库存代码获取每个仓库的库存金额,但返回的总金额不正确
你能帮我找错地方吗?
产品

+----------+-------------+------------+
| 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    |
+----------+-------------+------------+----------+-----------+

谢谢您

fnatzsnv

fnatzsnv1#

我用这个查询得到了正确的值

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

相关问题