如何从单个列中的多个列中获取所有结果 - SQL?

How to get all results from multiple columns in a single column - SQL?

我想将我的两个专栏中的所有结果放在一起。

我有一个 table my_table 像:

colA    colB
a       e 
b       e 
c       f
d       g

预期输出:

id      letter
1       a       
2       b       
3       c       
4       d       
5       e 
6       f 
7       g

我知道这可以通过 UNION 实现,但我对编号的工作方式有点困惑。我也很擅长没有 UNION 的解决方案。 letter 在输出中的顺序也无关紧要,但每个值都需要一个唯一的 ID。

我尝试了什么:

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
)