简体   繁体   English

多个表中的SQL sum和link字段

[英]SQL sum and link fields in multiple tables

Table 1 表格1

prodUID (*)    plantID   ItemName   size     qty
   1              1      car        med       5
   2              1      car        small     2
   3              1      car        large     8
   4              1      truck      small     7
   5              1      truck      med       0
   6              1      truck      large     4
   7              1      van        small     0
   8              2      truck      large    10

table2 表2

UID(*)  plantID  table2_plan_tid  table2_prodUID  itemname  size     num    wk
------------------------------------------------------------------------------
  1        1          1              car                    med        3    41
  2        1          2              car                    small      0    42    
  3        1          3              car                    large      6    41
  4        1          4              truck                  small      1    44
  5        1          5              truck                  med       10    45    
  6        1          6              truck                  large      1    43
  7        1          7              van                    small      7    42
  8        2          8              car                    med       10    41

table3 表3

UID(*)  plantid  table3_wk  table3_prodUID  itemName  size     qty
------------------------------------------------------------------
  1        1        41           2          car       med       5
  2        1        41           3          car       large     7
  3        1        43           7          van       small     8

Result I'm trying to get is 我想要得到的结果是

  • for plantid = ? 对于plantid =? and wk between ? 和周之间? and ? 和?
  • plantid = 1 and wk between 41 and 45 plantid = 1,且wk在41和45之间

  • sum qty for table1 on plantID plantID上table1的总数量

  • sum wk for table2 on plantID and for weeks plantID上的table2和ww的总和
  • sum qty on table with on plantID and for weeks 带有plantID的表的总数量和为数周

and some how link this with joins or subqueries to get 以及如何将其与联接或子查询链接以获得

plantid  Itemname  qty   num   order
------------------------------------
   1     car        15    9     12
   1     truck      11   12      0
   1     van         0    7      8

I can't seem to get the right outcome 我似乎无法得到正确的结果

I also considered putting in an itemNameID field in all tables 我还考虑过在所有表中放入一个itemNameID字段

if that would make it easier 如果那样会更容易

You can achieve what you want using this query: 您可以使用此查询实现所需的目标:

SELECT table1.plantID, table1.ItemName, SUM(table1.qty), COALESCE(t2.num_sum, 0), COALESCE(t3.qty_sum, 0)
FROM table1
LEFT JOIN (SELECT plantid, itemname, SUM(num) AS num_sum
      FROM table2
      WHERE wk >= 41 AND wk <= 45
      GROUP BY plantID, itemname) t2 ON table1.plantID = t2.plantID AND table1.ItemName = t2.ItemName
LEFT JOIN (SELECT plantid, itemname, SUM(qty) AS qty_sum
      FROM table3
      WHERE table3_wk >= 41 AND table3_wk <= 45
      GROUP BY plantID, itemname) t3 ON table1.plantID = t3.plantID AND table1.ItemName = t3.ItemName
WHERE table1.plantid = 1
GROUP BY plantID, ItemName

See also live! 另见直播!

Basically, you join 3 queries in order to get what you want. 基本上,您加入3个查询以获取所需的内容。 That would be a bit cleaner if you respect a certain harmony with you column names. 如果您尊重列名的一致性 ,那会更干净一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM