[英]How to get all results from multiple columns in a single column - SQL?
I want to get all the results in 2 of my columns together.我想将我的两个专栏中的所有结果放在一起。
I have a table my_table
like:我有一个表
my_table
像:
colA colB
a e
b e
c f
d g
Intended output:意向 output:
id letter
1 a
2 b
3 c
4 d
5 e
6 f
7 g
I know this is achievable with UNION
but I'm a bit confused about how the numbering would work then.我知道这可以通过
UNION
实现,但我对编号的工作方式感到有些困惑。 I'm good with a solution without UNION
too.我也很擅长没有
UNION
的解决方案。 The order of letter
in the output also doesn't matter, but each value needs a unique ID. output 中的
letter
顺序也无关紧要,但每个值都需要一个唯一的 ID。
What I tried:我尝试了什么:
SELECT
row_number() OVER (ORDER BY letter) AS id,
colA AS letter
FROM my_table
UNION
SELECT
row_number() OVER (ORDER BY letter) AS id,
colB AS letter
FROM my_table
SELECT row_number() OVER (ORDER BY letter) AS id, *
FROM (
SELECT DISTINCT colA AS letter
FROM my_table
UNION
SELECT DISTINCT colB AS letter
FROM my_table
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.