select tor.id as tw_o_r_id, case when t.send_to ='0' then null else t.send_to end tw_sn_to , t.display, serve, ser_type, ser_date, ser_dt_ent_dt, ack_user_id, dis_da_dt, da_rec_dt, ack_id, remark, l_ljs_rem, l_hjs_rem, l_ljs_p_d, l_hjs_p_d, l_ljs_pt, l_hjs_pt, t_ljs_p_d, t_hjs_p_d, t_ljs_rem, t_hjs_rem, station, weight, stamp, dis_remark, dispatch_user_id, dispatch_dt, dispatch_id, barcode, m_d, send_mail_dt , t.ent_dt_tw_tal_del entry_date_row_sac, case when (t.send_to ='0' and t.cp_sn_to ='0') then '0' when (t.send_to <>'0' and t.cp_sn_to ='0') then '2' else '2' end send_to_type ,case when t.send_to <>'0' then 1 else 0 end copy_type FROM tw_tal_del_sachin t inner join tw_tal_del td on td.tw_tal_del_demoas_id = t.id and td.process_id =t.process_id and t.branch =td.branch and t.rec_dt =td.rec_dt inner join tw_o_r tor on tor.tw_org_id = td.id inner join tw_comp_not_sachin tn on tn.tw_tal_del_id = t.id and tn.branch=t.branch where (t.send_to ='0' and t.cp_sn_to ='0') or (t.send_to <>'0' and t.cp_sn_to ='0') union select tor.id as tw_o_r_id, t.cp_sn_to as tw_sn_to , t.display, serve, ser_type, ser_date, ser_dt_ent_dt, ack_user_id, dis_da_dt, da_rec_dt, ack_id, remark, l_ljs_rem, l_hjs_rem, l_ljs_p_d, l_hjs_p_d, l_ljs_pt, l_hjs_pt, t_ljs_p_d, t_hjs_p_d, t_ljs_rem, t_hjs_rem, station, weight, stamp, dis_remark, dispatch_user_id, dispatch_dt, dispatch_id, barcode, m_d, send_mail_dt , t.ent_dt_tw_tal_del entry_date_row_sac, '2' send_to_type ,case when t.send_to <>'0' then 1 else 0 end copy_type FROM tw_tal_del_sachin t inner join tw_tal_del td on td.tw_tal_del_demoas_id = t.id and td.process_id =t.process_id and t.branch =td.branch and t.rec_dt =td.rec_dt inner join tw_o_r tor on tor.tw_org_id = td.id inner join tw_comp_not_sachin tn on tn.tw_tal_del_id = t.id and tn.branch=t.branch where (t.send_to ='0' and t.cp_sn_to <>'0') union select td.id as tw_o_r_id, null as tw_sn_to , t.display, serve, ser_type, ser_date, ser_dt_ent_dt, ack_user_id, dis_da_dt, da_rec_dt, ack_id, remark, l_ljs_rem, l_hjs_rem, l_ljs_p_d, l_hjs_p_d, l_ljs_pt, l_hjs_pt, t_ljs_p_d, t_hjs_p_d, t_ljs_rem, t_hjs_rem, station, weight, stamp, dis_remark, dispatch_user_id, dispatch_dt, dispatch_id, barcode, m_d, send_mail_dt , t.ent_dt_tw_tal_del entry_date_row_sac, '2' send_to_type ,case when t.send_to <>'0' then 1 else 0 end copy_type FROM tw_tal_del_sachin t inner join tw_tal_del td on td.tw_tal_del_demoas_id = t.id and td.process_id =t.process_id and t.branch =td.branch and t.rec_dt =td.rec_dt inner join tw_o_r tor on tor.tw_org_id = td.id inner join tw_comp_not_sachin tn on tn.tw_tal_del_id = t.id and tn.branch=t.branch where (t.send_to ='0' and t.cp_sn_to <>'0') union select tor.id as tw_o_r_id, t.send_to as tw_sn_to , t.display, serve, ser_type, ser_date, ser_dt_ent_dt, ack_user_id, dis_da_dt, da_rec_dt, ack_id, remark, l_ljs_rem, l_hjs_rem, l_ljs_p_d, l_hjs_p_d, l_ljs_pt, l_hjs_pt, t_ljs_p_d, t_hjs_p_d, t_ljs_rem, t_hjs_rem, station, weight, stamp, dis_remark, dispatch_user_id, dispatch_dt, dispatch_id, barcode, m_d, send_mail_dt , t.ent_dt_tw_tal_del entry_date_row_sac, '2' send_to_type ,case when t.send_to <>'0' then 1 else 0 end copy_type FROM tw_tal_del_sachin t inner join tw_tal_del td on td.tw_tal_del_demoas_id = t.id and td.process_id =t.process_id and t.branch =td.branch and t.rec_dt =td.rec_dt inner join tw_o_r tor on tor.tw_org_id = td.id inner join tw_comp_not_sachin tn on tn.tw_tal_del_id = t.id and tn.branch=t.branch where (t.send_to <>'0' and t.cp_sn_to <>'0') union select tor.id as tw_o_r_id, t.cp_sn_to tw_sn_to, t.display, serve, ser_type, ser_date, ser_dt_ent_dt, ack_user_id, dis_da_dt, da_rec_dt, ack_id, remark, l_ljs_rem, l_hjs_rem, l_ljs_p_d, l_hjs_p_d, l_ljs_pt, l_hjs_pt, t_ljs_p_d, t_hjs_p_d, t_ljs_rem, t_hjs_rem, station, weight, stamp, dis_remark, dispatch_user_id, dispatch_dt, dispatch_id, barcode, m_d, send_mail_dt , t.ent_dt_tw_tal_del entry_date_row_sac, '2' send_to_type ,case when t.send_to <>'0' then 1 else 0 end copy_type FROM tw_tal_del_sachin t inner join tw_tal_del td on td.tw_tal_del_demoas_id = t.id and td.process_id =t.process_id and t.branch =td.branch and t.rec_dt =td.rec_dt inner join tw_o_r tor on tor.tw_org_id = td.id inner join tw_comp_not_sachin tn on tn.tw_tal_del_id = t.id and tn.branch=t.branch where (t.send_to <>'0' and t.cp_sn_to <>'0')