简体   繁体   English

如何使用 Union All function 指定 BigQuery 中显示的数据顺序

[英]How to specify the order of data displayed in BigQuery using the Union All function

I am using the Union all function in Bigquery to display data from different tables.我在 Bigquery 中使用 Union all function 来显示来自不同表的数据。 Here is an example of what I am doing这是我正在做的一个例子

(Select ErrorCode AS `Code`, ErrorDate AS `Date`
From Errors
Where id = x
Order By ErrorDate ASC)

UNION ALL
(SELECT 'Error Code', 'Error Meaning')
UNION ALL
(SELECT '1001', 'Restart your PC')
UNION ALL
(SELECT '1002', 'Check the cables')

What I am trying to achieve here is a table that displays all the error codes an account has encountered, and then at the bottom a simple print of all the error codes we have in our system and their meaning.我在这里想要实现的是一个表格,显示帐户遇到的所有错误代码,然后在底部简单打印我们系统中的所有错误代码及其含义。

Now, when I run this code in BigQuery, for some reason the error codes and their meanings are showing up first, then the codes the system encountered.现在,当我在 BigQuery 中运行此代码时,由于某种原因,错误代码及其含义首先出现,然后是系统遇到的代码。 Example of result I am getting:我得到的结果示例:

Error Code | Error Meaning
1001       | Restart your PC
1002       | Check the Cables
------------------------------
1003       | 2022-09-12
1001       | 2218-09-04

I want the error codes the system encountered to show up first and then the meaning.我希望系统遇到的错误代码首先出现,然后是含义。 Something like this像这样的东西

Code       | Date
1003       | 2022-09-12
1001       | 2218-09-04
------------------------------
Error Code | Error Meaning
1001       | Restart your PC
1002       | Check the Cables

How can I specify this order in BigQuery?如何在 BigQuery 中指定此顺序? Moving the code portions up and down does not help.上下移动代码部分没有帮助。

Add another sorting column添加另一个排序列

(Select ErrorCode AS `Code`, ErrorDate AS `Date`,1 as _sort
From Errors
Where id = x
Order By ErrorDate ASC)

UNION ALL
(SELECT 'Error Code', 'Error Meaning',2)
UNION ALL
(SELECT '1001', 'Restart your PC',3)
UNION ALL
(SELECT '1002', 'Check the cables',4)
ORDER BY  _sort 

Try changing the order in the select尝试更改 select 中的顺序

Instead of:代替:

Select ErrorCode AS `Code`, ErrorDate AS `Date`

try:尝试:

Select ErrorDate AS `Date`, ErrorCode AS `Code`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM