L Xandor
L Xandor

Reputation: 1841

Oracle SQL combine output from two queries into single output

I am not able to create tables, but I want to combine the output from two queries into a single output. What I currently have

Output 1:

Date    Promo   Count
1-Aug   PROMO1  8
1-Aug   PROMO2  3
2-Aug   PROMO1  3
2-Aug   PROMO2  4

Output 2:

Date    Promo                 Count
1-Aug   DIFFERENTKINDOFPROMO1   3
2-Aug   DIFFERENTKINDOFPROMO1   5

What I'm trying to get (combine Output 1 and 2)

    Date    Promo   Count

    1-Aug   PROMO1  8
    1-Aug   PROMO2  3
    1-Aug   DIFFERENTKINDOFPROMO1   3
    2-Aug   PROMO1  3
    2-Aug   PROMO2  4
    2-Aug   DIFFERENTKINDOFPROMO1   5

As mentioned, I don't have the ability to create any tables, so I'm trying to do this on the fly so I get the final combined output.

Upvotes: 0

Views: 2273

Answers (3)

Gordon Linoff
Gordon Linoff

Reputation: 1269503

If the ordering matters, then this needs a bit of care:

select date, promo, count
from ((select date, promo, count, 1 as which from table1
      ) union all
      (select date, promo, count, 2 as which from table2
      )
     ) t
order by date, which;

It is not possible to guarantee the original ordering within each table (because there doesn't seem to be an ordering key for each table), but you can be sure that table 2 records follow table 1 on any given day.

Upvotes: 0

Amit
Amit

Reputation: 145

As mentioned above this would be the final query.. select * from table1 union all select * from table2 order by 1;

Upvotes: 0

Jon Roberts
Jon Roberts

Reputation: 2106

select * from table1
union all
select * from table2;

Upvotes: 2

Related Questions