-1

I have a problem how to combine the sql statements (inner join and union all. as a Newbie in SQL. Hopefully all members can help me to solve the problems.attached herewith the SQL. The leave_Trx and leave_History contain same values that need to be union. Thank you.

select m.name, t.startdt, t.enddt,t.noday,t.createdDT,
      (case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
      from leave_Trx t
      inner join leave_MType m on m.typeID = t.trxID
      inner join hr_personaldata b on b.pers_ID = @pers_ID
      where year(t.startdt) = @yyear
      and b.pers_ID = @pers_ID and b.pers_name LIKE '%'+@pers_name+'%'
      and b.pers_compID LIKE ''+@compID+''
      union all
      select * from leave_History h
      where year(h.startdt) = @yyear and h.status = 'A'
      ORDER BY t.startdt
user3678528
  • 1,741
  • 2
  • 18
  • 24

1 Answers1

0
 select  t.pers_ID,t.startdt, t.enddt,t.noday,t.createdDT,
      (case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
      from leave_Trx t
      inner join leave_MType m on m.typeID = t.pers_ID
      inner join hr_personaldata l on l.pers_ID = @pers_ID
      where year(t.startdt) = @yyear and t.status = 'A'
      union all
      select h.pers_ID, h.startdt, h.enddt,h.noday,h.createdDT,
      (case when h.approveST = 'Y' then 'Approved' when h.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST 
      from leave_History h
      inner join leave_MType m on m.typeID = h.pers_ID
      inner join hr_personaldata b on b.pers_ID = @pers_ID
      where year(h.startdt) = @yyear and h.status = 'A'