Использовать агрегирование с регистром:
select PO_No, Line_item, quantity, Value, PO_quantity, PO_value,
concat(
case when quantity=po_quantity then 'Quantity & PO_quantity match and negative value in Quantity '
else 'Quantity & PO_quantity unmatched and negative value in Quantity ' end,
case when min_quantity<0 then 'present' else 'not present' end
) as comments
from
(
select PO_No, Line_item ,
sum(quantity) quantity,
min(quantity) min_quantity,
sum(value) value,
min(po_quantity) po_quantity,
min(PO_value) PO_value
from
(--Use your table instead of this subquery
select stack(6, --PO_No Line_item Quantity Value PO_quantity PO_value
101, 10, 100, 5000, 70 , 5000,
101, 10, -30, -2000, 70 , 5000,
101, 20, 200, 7000, 50 , 1000,
101, 20, -50, -3500, 50 , 1000,
101, 30, 80 , 2000, 100, 3000,
101, 30, 40 , 1250, 100, 3000
) as (PO_No,Line_item,Quantity,Value,PO_quantity,PO_value)
) your_table
group by PO_No, Line_item
)s
order by PO_No, Line_item
Результат:
OK
101 10 70 3000 70 5000 Quantity & PO_quantity match and negative value in Quantity present
101 20 150 3500 50 1000 Quantity & PO_quantity unmatched and negative value in Quantity present
101 30 120 3250 100 3000 Quantity & PO_quantity unmatched and negative value in Quantity not present
Time taken: 136.559 seconds, Fetched: 3 row(s)
Просто замените подзапрос 'your_table' своей таблицей.