![](/img/trans.png)
[英]Combine columns from two tables AS one result and THEN use in SELECT query
[英]Combine the content of multiple columns from the result of a query
我的问题是能够将包含相似数据的 2 列的内容与具有相似数据的另外两列相结合。 问题是我想用相当长的查询的结果来做,所以不保存表来执行联合 function。
输入数据:
a栏 | b栏 | 列 c | d栏 |
---|---|---|---|
一个 | d | G | j |
b | e | H | ķ |
c | F | 一世 | l |
期望的结果:
a栏 | b栏 |
---|---|
一个 | d |
b | e |
c | F |
G | j |
H | ķ |
一世 | l |
一个通用的解决方案是union all
:
select a, b
from t
union all
select c, d
from t;
您可以使用 CTE 来表达这一点:
with cte as (
<subquery here>
)
select a, b
from cte
union all
select c, d
from cte;
我不确定这会比戈登的回答更容易实现,但另一种可能的方法如下:
WITH TwoRows AS
(SELECT 1 as RowNo UNION SELECT 2 as RowNo)
SELECT
CASE WHEN cte.RowNo = 1 THEN t.Column_A ELSE t.Column_C END AS Column_A
CASE WHEN cte.RowNo = 1 THEN t.Column_B ELSE t.Column_D END AS Column_B
FROM
TwoRows cte
CROSS JOIN MyTable t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.