MySql query ignore to show if Sum of rows Quantity condition is true

Issue

I am stuck on MySql Query execution! I created one table called ‘WO’. From this table, there are 3 columns

  1. Order QTY (Actual Quantity for the order)
  2. Receive Qty (Actual Quantity that has been received)
  3. Pending Qty. (Quantity that will be received)

I can add some of my row results. But my target is, if Order QTY=Receive QTY, then this row will not be shown during execution. But I failed to capture this part! Need your valuable advice to solve this part.

Below is my code and output in a picture.

SELECT 
    id,
    ind,
    WONO,
    PRODUCT,
    SUM(QTY) as "Order_QTY",
    SUM(Rec_QTY) as "Received",
    SUM(QTY-Rec_QTY) as "Pending",
    UNIT 
FROM 
    wo 
WHERE 
    Inhouse_Status!="IN-HOUSED" 
Group by 
    ind 
ORDER BY 
    ind DESC

enter image description here

Solution

You may filter rows using a where condition eg WHERE Order QTY <> Receive QTY or since you are using a group by to sum total Order QTY you may filter the total using a having clause eg WHERE SUM(QTY) <> SUM(Rec_QTY).

Your query could look like:

SELECT 
    ind,
    WONO,
    PRODUCT,
    SUM(QTY) as "Order_QTY",
    SUM(Rec_QTY) as "Received",
    SUM(QTY-Rec_QTY) as "Pending",
    UNIT 
FROM 
    wo 
WHERE 
    Inhouse_Status!="IN-HOUSED" 
Group by 
    ind, WONO,PRODUCT, UNIT
HAVING
    SUM(QTY) <> SUM(Rec_QTY)
ORDER BY 
    ind DESC

Answered By – ggordon

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published