2011-10-05 37 views
1

我HV一個表是這樣的: -如何分組?

Purchase_order Supplier_number Item_number Quantity Date 
7896   WE345  QAZWS002022  1242   20110131 
7896   WE345  QAZWS002324  1936   20110131 
7896   WE345  QAZWS002526  1111   20110131 
7896   WE345  QAZWS002729  414    20110131 
7896   WE345  QAZWS002022  582   20110131 
7896   WE345  QAZWS002022  1   20110131 
7896   WE345  QAZWS002324  1436   20110131 
7896   WE345  QAZWS002324  1   20110131 
7896   WE345  QAZWS002526  634   20110131 
7896   WE345  QAZWS002729  92   20110131 

我嘗試使用這個SQL: -

SELECT Purchase_order, 
     Supplier_number, 
     Date , 
     substring(Item_number, 1, 5) MainProduct, 
     SUM(Quantity)    Qty 
FROM tableA 
WHERE Purchase_order = '7896' 
GROUP BY Purchase_order, 
      Supplier_number, 
      Item_number, 
      Date 

,我收到的輸出如下: -

Purchase_order Supplier_number Item_number Quantity Date 
7896   WE345  QAZWS   242   20110131 
7896   WE345  QAZWS   1936   20110131 
7896   WE345  QAZWS   1111   20110131 
7896   WE345  QAZWS   414   20110131 
7896   WE345  QAZWS   582   20110131 
7896   WE345  QAZWS   1   20110131 
7896   WE345  QAZWS   1436   20110131 
7896   WE345  QAZWS   1   20110131 
7896   WE345  QAZWS   634   20110131 
7896   WE345  QAZWS   92   20110131 

我的預期輸出應該如下所示: -

Purchase_order Supplier_number Item_number Quantity Date 
7896   WE345  QAZWS   7449   20110131 

任何人都可以幫助我調整SQL命令嗎?

謝謝。

+2

詢問你應該知道如何將它們自己格式化42個問題之後。 –

回答

2
select Purchase_order, Supplier_number, Date, substring(Item_number,1,5) MainProduct, sum(Quantity) Qty from tableA 
where Purchase_order = '7896' 
group by Purchase_order, Supplier_number, substring(Item_number,1,5), Date 
1

你需要把你的函數上Item_number你的小組是:

select Purchase_order, Supplier_number, Date, substring(Item_number,1,5) MainProduct, sum(Quantity) Qty 
from tableA 
where Purchase_order = '7896' 
group by Purchase_order, Supplier_number, substring(Item_number,1,5), Date