如何在 Snowflake 中选择具有虚假数据的多行

Mar*_* C. 3 sql unit-testing snowflake-cloud-data-platform

在Snowflake中,是否有一种更简单的方法可以选择内存中的多行假数据而不将实际数据加载到表中?下面是一个示例查询,显示我当前如何生成包含多行假数据的对象。

with
  fake_row_1 as (
    select
      1 as num,
      'one' as txt
  ),
  fake_row_2 as (
    select
      2 as num,
      'two' as txt
  ),
  fake_row_3 as (
    select
      3 as num,
      'three' as txt
  ),
  fake_table as (
    select * from fake_row_1 union
    select * from fake_row_2 union
    select * from fake_row_3
  )
select *
from fake_table
Run Code Online (Sandbox Code Playgroud)

我正在尝试测试对查询逻辑的更改,而不是将测试数据加载和卸载到测试表中,而是尝试在内存中暂存一个假表以更快地验证预期结果。

理想情况下,我能够运行类似于以下内容的查询。

with
  fake_table as (
    select
      columns (num, txt)
      values (1, 'one'),
             (2, 'two'),
             (3, 'three')
  )
select *
from fake_table
Run Code Online (Sandbox Code Playgroud)

小智 5

你能在 CTE 中进行工会吗?

with
  fake_rows as (
    select
      1 as num,
      'one' as txt
  union
    select
      2,
      'two'
  union
    select
      3,
      'three'
  )
select *
from  fake_rows 
Run Code Online (Sandbox Code Playgroud)

这可能会更干净一些:

with
  fake_rows as (
    select $1 AS txt,
           $2 as num
    FROM 
    (VALUES  
     (1,'one'),
     (2,'two'),
     (3,'three') 
         )) 
select * from  fake_rows
Run Code Online (Sandbox Code Playgroud)