'Merging 6 tables having same columns in sql
I have 6 tables with columns cust_id and cust_preference. I need to merge the information from these 6 tables under one such that I am able to view which of the cust_ids have multiple preferences. I am getting confused as to which table should I take as the base table and how the query would look like? Also, would it make any difference in the query if it is run in BIG Query?
I used UNION ALL
Example with 3 tables below enter image description here
Expected Outcome enter image description here
Solution 1:[1]
Identical Tables (why do you have them anyway) are joined vertically by UNION
So you get
SELECT col1, col2,col3 FROM a1
UNION
SELECT col1, col2,col3 FROM a2
UNION
SELECT col1, col2,col3 FROM a3
UNION
SELECT col1, col2,col3 FROM a4
UNION
SELECT col1, col2,col3 FROM a5
UNION
SELECT col1, col2,col3 FROM a6
This would also remove all duplicates, if there are any.
The Order is irrelevant, and you can take the unioned table to sort or Group data as you need it
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Ergest Basha |