Home > Back-end >  How to print a query statement
How to print a query statement

Time:04-27

I am working on two different tables names sales and inventory. From the table inventory, there's Quantity_Received, from the table sales, there's Quantity_Sold 'Quantity Remaining in store' represent the difference between Quantity_Received and Quantity_Sold

When the 'Quantity Remaining in store' is equal to 0, I want my sql to print 'out of stock' When the 'Quantity Remaining in store' is greater than0 but less than 10 , I want my sql to print 'low stock' and When the 'Quantity Remaining in store' is greater than 10, I want my sql to print 'Still in stock'

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'

CodePudding user response:

Something like

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
  • Related