2017-04-25 45 views
0

表A列出了必須經過4個可能站點中的每個站點的項目和數量。加入2個表格的準確結果

Item  To Cut  To Paste  To Laminate  To Ship 
Box   9   9    5    9 
Cart   1   0    10    10 

表B列出了已經通過每個工作站處理的件數。

Item  Cut Done  Paste Done  Laminated  Shipped 
Box   9   0    0    0 
Box   0   9    0    0 
Box   0   0    5    0 
Box   0   0    0    9 

當我加入他們的行列它表明:

Item  To Cut  Cut Done  To Paste  Paste Done 
Box   36   9    36    9 

這是因爲該方式表B的作品。我怎樣才能準確地顯示一個表格中的總切削和切削完成率,因此百分比是正確的?

我的查詢是:

select a.item, a.to_cut, a.to_paste, a.to_laminate, a.to_ship, b.cut_done, b.paste_done, b.laminated, b.shipped 

from ToDo a join Done b on a.item = b.item 
+0

請添加您的當前查詢。 – McNets

+0

已添加。謝謝。 –

回答

1

您應該添加一個子查詢與計算出的數額。

select tableA.Item, To_Cut, To_Paste, To_Laminate, To_Ship, d.Cut_Done, d.Paste_Done, d.Laminated, d.Shipped 
from tableA 
inner join (select Item, 
        sum(Cut_Done) as Cut_Done, 
        sum(Paste_Done) as Paste_Done, 
        sum(Laminated) as Laminated, 
        sum(Shipped) as Shipped 
      from tableB 
      group by Item) d 
on tableA.Item = d.Item; 

| Item | To_Cut | To_Paste | To_Laminate | To_Ship | Cut_Done | Paste_Done | Laminated | Shipped | 
|-----:|--------|----------|-------------|---------|----------|------------|-----------|---------| 
| Box | 9  | 9  | 5   | 9  | 9  | 9   | 5   | 0  | 

Rextester here

+0

非常感謝您對此的快速幫助。 –

+0

我很高興能夠提供幫助。 – McNets