我正在處理兩個不同的表,名稱為 sales 和 inventory。從表inventory,有Quantity_Received,從表sales,有Quantity_Sold 'Quantity Remaining in store'代表Quantity_Received和Quantity_Sold之間的差異
當 'Quantity Remaining in store' 等于 0 時,我希望我的 sql 列印 'out of stock' 當 'Quantity Remaining in store' 大于 0 但小于 10 時,我希望我的 sql 列印 'low stock'并且當“商店剩余數量”大于 10 時,我希望我的 sql 列印“仍有庫存”
SELECT
inventory.Quantity_Received AS 'Q1',
sales.Quanity_Sold as 'Q2',
inventory.Quantity_Received - sales.Quanity_Sold as 'Quantity Remaining in Store'
from inventory
inner join sales on inventory.id=sales.id
if (inventory.Quantity_Received - sales.Quanity_Sold =>10),
BEGIN,
print 'Still enough in Stock',
END
ELSE IF (inventory.Quantity_Received - sales.Quanity_Sold =<10)
begin
print 'Low in Stock'
END
if (inventory.Quantity_Received - sales.Quanity_Sold =0),
begin,
print 'Out of stock'
uj5u.com熱心網友回復:
就像是
SELECT
inventory.Quantity_Received AS 'Q1',
sales.Quanity_Sold as 'Q2',
inventory.Quantity_Received - sales.Quanity_Sold as 'Quantity Remaining in Store',
CASE
WHEN (inventory.Quantity_Received - sales.Quanity_Sold >10) THEN 'Still enough in Stock'
WHEN (inventory.Quantity_Received - sales.Quanity_Sold =<10 AND inventory.Quantity_Received - sales.Quanity_Sold > 0) THEN 'Low in Stock'
ELSE 'Out of Stock'
END as Quantity_Remaining_in_Store
from inventory
inner join sales on inventory.id=sales.id
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/465650.html
上一篇:sql連接和求和3
