q1: select distinct y.c1,y.c2,y.c6, y.c3,y.c5 max(case when y.c3 is null and y.c4=\'N\' and y.c5 <>\'Cancelled\' and p.c6 is not null then \'Y\' el