SQL从一列中的两列获取记录
我试图从两列中获取记录,以便输出包含来自第一列的1条记录,然后是来自第二列的1条记录 塞纳里奥就是这样SQL从一列中的两列获取记录,sql,sql-server,Sql,Sql Server,我试图从两列中获取记录,以便输出包含来自第一列的1条记录,然后是来自第二列的1条记录 塞纳里奥就是这样 ColA ColB 1 a 2 b 3 c Output 1 a 2 b 3 c 演示:这里是一种没有窗口函数的方法,假设ColA指定了行的顺序: select output from ((select cola as output, cola as ordering, 'a' as which from table t
ColA ColB
1 a
2 b
3 c
Output
1
a
2
b
3
c
演示:这里是一种没有窗口函数的方法,假设ColA指定了行的顺序:
select output
from ((select cola as output, cola as ordering, 'a' as which
from table t
) union all
(select colb, cola, 'b'
from table t
)
) t
order by ordering, which;
请注意,这将保证结果集中的值是交错的。您试过了吗?谢谢Tim,但结果很接近,但我没有得到确切的结果。我得到的结果集是1AB23CD4,而我想要的是1A2C
select output
from ((select cola as output, cola as ordering, 'a' as which
from table t
) union all
(select colb, cola, 'b'
from table t
)
) t
order by ordering, which;