select DISTINCT
case when c='x' then x.date else y.date end date,
case when c='x' then x.client else y.client end client,
case when c='x' then x.amount else y.amount end amount,
c from_
from tablex x
join tabley y on x.date=y.date and x.client=y.client
cross join (select 'x' union all select 'y') z(c)
order by date, client, from_;
公认解决方案的替代方案。
请注意,如果您有来自任一/两个表的多个匹配项,则来自“x”的所有结果将列在来自“y”的匹配项之前。例如
create table tablex (Date int, Client char(3), Amount int);
insert tablex select
123 ,'abc', 123 union all select
456 ,'abc', 987 union all select
123 ,'abc', 919 union all select
234 ,'xyz', 567;
create table tabley (Date int, Client char(3), Amount int);
insert tabley select
123 ,'abc', 234 union all select
123 ,'abc', 867 union all select
921 ,'lka', 981 union all select
234 ,'xyz', 123;
**Results**
date client amount from_
----------- ------ ----------- -----
123 abc 123 x
123 abc 919 x
123 abc 234 y
123 abc 867 y
234 xyz 567 x
234 xyz 123 y