'How to merge 2 SQL query to create a single query to use as a custom query in tableau? [closed]
I have 2 custom SQL which was used to create a single view in tableau, In both the queries main tables, join and where condition are same in both...only difference is some calculation in both queries is select part. HOW Can I merge both query to create a single SQL query to create a same view of dashboard in tableau.
Solution 1:[1]
you can combine them into 1 SQL - you can use UNION ALL or CASE-WHEN.
CASE-WHEN
if you dont want to duplicate your data and number of columns arent same in qry1 and qry2, you can combine both queries into one query using case when. Depending on case for query 1 you will use calculation of query1 and so on.
Whole query should look like this -
select
...
case when <condition for query1> then <calculation for query1>
when <condition for query2> then <calculation for query>
end as amount_calculaetd
from
mytables
UNION ALL
If number of columns, data type in both queries are same you can use this. But this will have other issues like data can be duplicated. So decide according to union output. Your sql should look like this
(query 1)
union all
(query 2)
I prefer first solution but again, it all depends on what exactly is needed for your query. First solution will generate less rows, optimized but you need to find out what is different in query1 and query2.
Whereas second one will simply union both data set so this will generate more rows, may take more time and can cause data duplication.
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 | Koushik Roy |
