在雪花查询中创建空列并定义数据类型
我有一个带有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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试将
null
文字施放到所需的类型,例如You could try to cast the
NULL
literals to the type you want, e.g.类型铸造将清除数据类型问题。

Type casting will clear the data type problem.

null设计的null具有文本数据类型:
set Operator union all 匹配列将受到隐式铸造(“ cocrecion”)。
为了避免这种情况,零值应明确施放于适当的数据类型:
NULL by design has TEXT data type:
When set operator UNION ALL is applied the matching column will be subject to Implicit Casting (“Coercion”).
In order to avoid it, NULL value should be explicitly casted to proper data type: