案例 如果您不想重复数据并且 qry1 和 qry2 中的列数不同,您可以使用 case when 将两个查询合并为一个查询。根据查询 1 的情况,您将使用查询 1 的计算,依此类推。 整个查询应如下所示 -
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 如果两个查询中的列数、数据类型相同,则可以使用它。但这会带来其他问题,例如数据可能会重复。所以根据工会的产出来决定。你的sql应该看起来像这样
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.
发布评论
评论(1)
您可以将它们组合成 1 个 SQL - 您可以使用
UNION ALL
或CASE-WHEN
。案例
如果您不想重复数据并且 qry1 和 qry2 中的列数不同,您可以使用 case when 将两个查询合并为一个查询。根据查询 1 的情况,您将使用查询 1 的计算,依此类推。
整个查询应如下所示 -
UNION ALL
如果两个查询中的列数、数据类型相同,则可以使用它。但这会带来其他问题,例如数据可能会重复。所以根据工会的产出来决定。你的sql应该看起来像这样
我更喜欢第一个解决方案,但同样,这一切都取决于你的查询到底需要什么。第一个解决方案将生成更少的行,经过优化,但您需要找出 query1 和 query2 中的不同之处。
而第二个将简单地合并两个数据集,因此这将生成更多行,可能需要更多时间并可能导致数据重复。
you can combine them into 1 SQL - you can use
UNION ALL
orCASE-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 -
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
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.