0

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.

3 Answers 3

2
select * from table1
union all
select * from table2;
Sign up to request clarification or add additional context in comments.

2 Comments

and if you need it sorted SELECT * FROM(SELECT ... UNION..) ORDER BY 1
You don't even need the subquery. Just put ORDER BY 1 at the end of the answer query (moving the ;).
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.