sql - BigQuery - 创建视图时如何使用嵌套列更改架构的顺序?
问题描述
我想更改嵌套在我的 VIEW 中的架构。
但 BigQuery 不会这样做,因为我将记录称为“productPrice”。
确实,如果我不调用它,我就无法将它嵌套在我的视图中。
使用“productPrice”时出现错误消息
Column xx in UNION ALL has incompatible types: STRUCT<type STRING, price DOUBLE, currency STRING, ...>, STRUCT<taxRate DOUBLE, taxType STRING, priceStartDate STRING, ...> at [xx:x]
表格1
productPrice RECORD NULLABLE
productPrice.type STRING NULLABLE
productPrice.price FLOAT NULLABLE
productPrice.currency STRING NULLABLE
productPrice.priceStartDate STRING NULLABLE
productPrice.taxRate FLOAT NULLABLE
productPrice.taxType STRING NULLABLE
表2
productPrice RECORD NULLABLE
productPrice.taxRate FLOAT NULLABLE
productPrice.taxType STRING NULLABLE
productPrice.priceStartDate STRING NULLABLE
productPrice.currency STRING NULLABLE
productPrice.price FLOAT NULLABLE
productPrice.type STRING NULLABLE
请求 productPrice
CREATE VIEW product_view AS
SELECT
productPrice,
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type,
from table1
UNION ALL
SELECT
productPrice,
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type,
FROM table2
要求没有 productPrice
CREATE VIEW product_view AS
SELECT
--productPrice,
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type,
from table1
UNION ALL
SELECT
--productPrice,
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type,
FROM table2
视图中没有“productPrice”的结果
type STRING NULLABLE
taxRate FLOAT NULLABLE
taxType STRING NULLABLE
priceStartDate STRING NULLABLE
currency STRING NULLABLE
price FLOAT NULLABLE
解决方案
以下是 BigQuery 标准 SQL
#standardSQL
SELECT
STRUCT(
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type
) AS productPrice
FROM table1
UNION ALL
SELECT
STRUCT(
productPrice.taxRate,
productPrice.taxType,
productPrice.priceStartDate,
productPrice.currency,
productPrice.price,
productPrice.type
)
FROM table2
推荐阅读
- java - JAXB 将内存大小 XML 减少为 Object
- wso2 - 如何在容器环境中更改 WSO2 API Manager 3.1.0 中的日志级别?
- sql - 从开始日期到结束日期的每个日期的行
- html - 如何为特定文件夹编写异常
- java - 由于 [ERR 0512] 由于 Websphere 9 中的样式表错误,XsltPayloadTransformer 的 Bean 创建问题无法继续
- python - 按值过滤和除列
- django - 来自数据库的 Django 管理面板选择
- php - 用php解析谷歌提要文件中的节点?
- java - Spring Boot 中与 Apache Kafka 的 Json 消息通信
- terminal - 在 gdb 中产生字符的箭头键