SQL连接两个类似的表

时间:2016-02-23 07:28:51

标签: sql db2 union union-all

我有两张相似的表格:

select * from tab
where  -- last five months
   business_date >= add_months(trunc(current_date),-5)
qualify
   row_number()  
   over (partition by trunc(business_date)  -- every month
         order by business_date desc, load_timestamp desc) -- latest date

我想要这个结果:

SELECT * FROM CTP0002;

SDKEY      SDNAME
2          Hello
4          World


SELECT * FROM CTP0003;

SSKEY      SSNAME
3          Name1
6          Name2

我试图加入它,但这不起作用。

1 个答案:

答案 0 :(得分:3)

试试这个

SELECT * FROM CTP0002;
union all
SELECT * FROM CTP0003;