2016-10-11 65 views
1

我想測試一些邏輯,在BigQuery中使用虛擬表和數據。我已經成功使用子查詢上的通配符

WITH dummy AS (SELECT 1 AS a) 
SELECT a FROM dummy 

這樣,通過使用WITH語句比較複雜,我可以嘗試在這個虛擬表中的一些不同的邏輯。

現在我碰壁了,因爲我有一些數據在不同的分區中,我想測試。使用標準的SQL我可以這樣做:

SELECT a 
FROM 
    `dummy_*` 
WHERE 
    _TABLE_SUFFIX BETWEEN '20161001' AND '20161003' 

但是我真的很希望能夠做的子查詢同樣在WITH子句:

WITH 
    dummy_20161001 AS (SELECT 1 AS a UNION ALL SELECT 4 AS a), 
    dummy_20161002 AS (SELECT 7 AS a UNION ALL SELECT 10 AS a) 
SELECT a FROM dummy_* 
WHERE 
    _TABLE_SUFFIX BETWEEN '20161001' AND '20161003' 

有什麼我可以代替_TABLE_SUFFIX與做到這一點,還是有任何其他方式來創建表分區的虛擬表示?

回答

1

在我看來,下面可以滿足您的測試需求。至少這是一個選項:O)

WITH 
    dummy_20161001 AS (SELECT 1 AS a UNION ALL SELECT 2 AS a), 
    dummy_20161002 AS (SELECT 3 AS a UNION ALL SELECT 4 AS a), 
    dummy_20161003 AS (SELECT 5 AS a UNION ALL SELECT 6 AS a), 
    dummy_20161004 AS (SELECT 7 AS a UNION ALL SELECT 8 AS a), 
    `dummy_*` as (
    SELECT *, '20161001' as _TABLE_SUFFIX FROM dummy_20161001 UNION ALL 
    SELECT *, '20161002' as _TABLE_SUFFIX FROM dummy_20161002 UNION ALL 
    SELECT *, '20161003' as _TABLE_SUFFIX FROM dummy_20161003 UNION ALL 
    SELECT *, '20161004' as _TABLE_SUFFIX FROM dummy_20161004 
) 
SELECT a 
FROM `dummy_*` 
WHERE 
    _TABLE_SUFFIX BETWEEN '20161001' AND '20161003' 
+0

這看起來不錯:)我跑這個,它的工作。你能否澄清一下,'dummy_ *'表不會在BQ中創建,所以我不需要在之後刪除? – binnisb

+1

沒錯 - 沒有在這裏創建表格 –

0

_TABLE_SUFFIX和(如果使用的是分區表 - 僅在BigQuery的的_PARTITIONTIME)僞列工作管理的表。

是否可以將樣例數據寫入多個表或單個分區表(使用SELECT查詢與destination_table)?之後,你應該能夠使用僞列。