if statement - DB2 SQL filter query result by evaluating an ID which has two types of entries -
after many attempts have failed @ , hoping can help. query returns every entry user makes when items made in factory against , order number. example
order number entry type quantity 3000 1 1000 3000 1 500 3000 2 300 3000 2 100 4000 2 1000 5000 1 1000
what want query return filter results this
if order number has entry type 1 , 2 return row type 1 otherwise return row whatever type order number.
so above end up:
order number entry type quantity 3000 1 1000 3000 1 500 4000 2 1000 5000 1 1000
currently query (db2, in basic terms looks ) , correct until change request came through!
select * bookings type=1 or type=2
thanks!
select * bookings left outer join ( select order_number, max(case when type=1 1 else 0 end) + max(case when type=2 1 else 0 end) type_1_and_2 bookings group order_number ) has_1_and_2 on type_1_and_2 = 2 has_1_and_2.order_number = bookings.order_number bookings.type = 1 or has_1_and_2.order_number null
find orders have both type 1 , type 2, , join it.
if row matched join, return if type 1 if row did not match join (has_type_2.order_number null
) return no matter type is.