J
J
JediKhan2021-06-25 18:07:02
SQL
JediKhan, 2021-06-25 18:07:02

How to merge two, three or more tables in SQL?

For example I have 3 tables:
Table 1 (contains source A data): Date, Campaign, Ad, Impression, Click, Cost
Table 2 (contains source B data): DateTime, Campaign, Ad, Impression, Click, Cost
Table 3 ( contains data from different sources): Date, Source, Campaign, Ad, App Install ,

Purchase
(Cost), SUM(App Install), SUM(Purchase)

Note:
sourse in table 3 can be greater than 2
date field example: '2021-05-01',
datetime field example '2021-05-01 12:31:47 '

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
galaxy, 2021-06-25
@DenisBezukhov

more or less like this

select `Date`, source, Campaign, Ad, SUM(Click), SUM(Cost), SUM(`App Install`), SUM(Purchase)
  from (
    select `Date`, 'source A' as source,  Campaign, Ad, Click, Cost, null as `App Install`, null as Purchase from table1
    union
    select date(`DateTime`), 'source B', Campaign, Ad, Click, Cost, null, null from table2
    union
    select `Date`, Source, Campaign, Ad, null, null, `App Install`, Purchase from table3
  ) t
group by `Date`, source, Campaign, Ad

Z
zexer, 2021-06-25
@zexer

Read about JOIN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question