2016-10-31 39 views
-1

復原通知我有一個store表具有productNameproductNumberstoredInBranchblockNumberquantitynotifyInSQL查詢來從存儲表

如果quantity小於notifyIn它會通知我,我想下面的查詢:

select * from store 
where (quantity < notifyIn) AND (notify > 0) 

這完美的作品,但由於一個產品可以存儲在多個blockNumbers它通知我即使量的產品並不比notifyin少,

例如:

productName = monitor, productNumber=123, storedInBranch=kenya, blocknumber=5b, quantity=5, notifyin=4 

productName = monitor, productNumber=123, storedInBranch=kenya, blocknumber=<b>1a</b>, quantity=5, notifyin=4 

所以ABOV e查詢返回的結果都是低的,我想要做的就是總計quantity(5 + 5)和notifyIn 4.

+0

考慮進一步標準化數據庫模式以刪除重複字段。你可以提取產品到自己的表 – useyourillusiontoo

+0

也提取分支到一個表與相關的分支信息,如通知限制 – useyourillusiontoo

回答

1

您在這裏依賴notifyin爲多行中的相同值。你可以做類似

SELECT productNumber, sum(quantity), min(notifyin) 
FROM store 
GROUP BY productNumber 
HAVING sum(quantity) < min(notifyin) 

如果可能的話,我會鼓勵你改變你的模式,以更好地反映你的域邏輯。也許有一個單獨的表與產品編號並通知價值。這樣你就不會複製notifyin,並在不同的行中存儲不同的值。

+0

很高興聽到它的幫助。如果它能幫助你回答你的問題,請接受我的回答。 – RVid

+0

閱讀以下內容:[當有人回答我的問題時,我應該怎麼做?](https://stackoverflow.com/help/someone-answers)。 –