在雪花查询中创建空列并定义数据类型

发布于 2025-02-08 00:46:29 字数 578 浏览 2 评论 0原文

我有一个带有15列的View1,我想将其与具有100列的View2结合。 View1中的15列与View2中的100列中的15列对齐,但是从我的理解中,除非它们具有相同的列,否则我不能在雪花上结合2个表或视图。

取而代之的是,我尝试将带有空值的其他85列添加到View1并在View1查询中定义它,但是这些85列丢失的列的数据类型各不相同(布尔,字符串,整数,日期等)。我可以写下这样的东西:

CREATE VIEW "VIEW1" AS
SELECT FIELD1
      ,FIELD2
      ,...
      ,FIELD15
      ,NULL AS FIELD16
      ,NULL AS FIELD17
      ,...
      ,NULL AS FIELD100
FROM SOURCE_TABLE

但是雪花自动分配了16-100的字段,即varchar数据类型,当我转到Union View1和View2时,它仍然会造成问题。有没有办法在查询中定义数据类型?有点像“ null as field16 boolean ”?如果没有,是否有另一种方法(或更好的方法)这样做?不幸的是,我无法更改源表。

I have a View1 with 15 columns and I would like to Union it to View2 which has 100 columns. The 15 columns in View1 align with 15 of the 100 columns in View2 but from my understanding, I can't Union 2 tables or views in Snowflake unless they have the same set of columns.

Instead, I'm trying to add the other 85 columns with a NULL value to View1 and define it in the View1 query but the data types for those 85 missing columns vary (boolean, string, integer, date, etc). I can write something like this:

CREATE VIEW "VIEW1" AS
SELECT FIELD1
      ,FIELD2
      ,...
      ,FIELD15
      ,NULL AS FIELD16
      ,NULL AS FIELD17
      ,...
      ,NULL AS FIELD100
FROM SOURCE_TABLE

But Snowflake is automatically assigning Fields 16-100 a VARCHAR data type which will still create a problem when I go to union View1 and View2. Is there a way to define the data type in the query? Sort of like "NULL AS FIELD16 BOOLEAN"? If not, is there another way (or better way) of doing this? I can't alter the source table, unfortunately.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒夜笙歌凉 2025-02-15 00:46:30

您可以尝试将null文字施放到所需的类型,例如

CREATE VIEW "VIEW1" AS
SELECT FIELD1,
       FIELD2,
       ...
       FIELD15,
       CAST(NULL AS BOOLEAN) AS FIELD16,
       ...
FROM SOURCE_TABLE;

You could try to cast the NULL literals to the type you want, e.g.

CREATE VIEW "VIEW1" AS
SELECT FIELD1,
       FIELD2,
       ...
       FIELD15,
       CAST(NULL AS BOOLEAN) AS FIELD16,
       ...
FROM SOURCE_TABLE;
温折酒 2025-02-15 00:46:30

类型铸造将清除数据类型问题。

Type casting will clear the data type problem.
enter image description here

日记撕了你也走了 2025-02-15 00:46:29

有没有办法在查询中定义数据类型?有点像“ null as field16 boolean”?

null设计的null具有文本数据类型:

CREATE OR REPLACE VIEW v1 
AS
SELECT NULL AS c;

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ILIKE 'v1';
-- DATA TYPE
-- TEXT

set Operator union all 匹配列将受到隐式铸造(“ cocrecion”)

一般用法注释

确保每列的数据类型在不同来源的行之间保持一致。

为了避免这种情况,零值应明确施放于适当的数据类型:

CREATE VIEW "VIEW1" AS
SELECT FIELD1
      ,FIELD2
      ,...
      ,FIELD15
      ,NULL::BOOLEAN AS FIELD16
      ,NULL::BOOLEAN AS FIELD17
      ,...
      ,NULL::INT AS FIELD100
FROM SOURCE_TABLE

Is there a way to define the data type in the query? Sort of like "NULL AS FIELD16 BOOLEAN"?

NULL by design has TEXT data type:

CREATE OR REPLACE VIEW v1 
AS
SELECT NULL AS c;

SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME ILIKE 'v1';
-- DATA TYPE
-- TEXT

When set operator UNION ALL is applied the matching column will be subject to Implicit Casting (“Coercion”).

General Usage Notes

Make sure that the data type of each column is consistent across the rows from different sources.

In order to avoid it, NULL value should be explicitly casted to proper data type:

CREATE VIEW "VIEW1" AS
SELECT FIELD1
      ,FIELD2
      ,...
      ,FIELD15
      ,NULL::BOOLEAN AS FIELD16
      ,NULL::BOOLEAN AS FIELD17
      ,...
      ,NULL::INT AS FIELD100
FROM SOURCE_TABLE
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文