简体   繁体   English

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

[英]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.

相关问题 如何比较 Bigquery 中的多个表并将结果放在不同的列中? - How can I compare multiple tables in Bigquery and get the results in in separate columns? 如何将一个表中的多行插入到另一个表的单行的结构列中? - How do I insert multiple rows from one table into a struct column of a single row of another table? 如何在 BigQuery 中将所有列除以一列 - How to divide all columns by one column in BigQuery 如何根据在另一个集合中执行的 Streambuilder 查询的结果将所有集合文档数据放入列表视图 - How to get all collection document data into a listview based on results from Streambuilder Query performed in another collection 如何将 2 列中的值与 1 列连接起来? - SQL - How to join values in 2 columns with 1 column? - SQL SQL - 根据其他列的值重命名一列 - SQL - Rename one column based on values from other columns 如何从发送网格获取发送给多个收件人的单个 email 的 email 活动状态 - How to get the email activity status from send grid for a single email sent to multiple recipients 使用group-by时如何将所有列聚合到SQL中的数组中? - How to aggregate all columns into array in SQL when using group-by? 在单个事务中通过 id 从 firestore 获取多个文档 - get multiple docs from firestore by id in single transaction 如何在 Azure 数据工厂中通过一次调用获取所有管道活动 - How to get all pipelines activities in a single call in Azure data factory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM