2014-11-04 63 views
1

我嘗試獲取答案數據庫,該數據庫爲我提供目的地美國和不同來源國家的所有數據。然而,一行中可能會寫CN,HK,JP--意思是很多事情。所以,我寫的查詢如下,但答案僅包含來源CN或HK,但不包含「CN,JP,HK」。
什麼是正確的代碼?大查詢 - 和/或運營商

SELECT destination_country,origin_country, createDate FROM [DataWarehouse.Draft] 
Where destination_country contains "US" 
And originCountries In ("CN", "HK") 

行originCountries destinationCountries createWeek
1 CN US 2014W30
2 CN US 2014W30
3 CN US 2014W30
4 CN US 2014W30
5 HK US 2014W30
6 HK US 2014W30

回答

2

這確實是一個AND/OR問題。

嘗試:

SELECT destination_country,origin_country, createDate FROM [DataWarehouse.Draft] 
Where destination_country contains "US" 
And (originCountries CONTAINS "CN" OR originCountries CONTAINS "HK") 
4

您的origin_country數據目前平鋪在originCountries字段中。您需要將此(拼合)字段擴展爲重複字段。我不知道你的確切架構,但沿線的東西:

SELECT * FROM 
    (SELECT destination_country, SPLIT(originCountries, ",") as origin_country, 
    createDate FROM [DataWarehouse.Draft]) 
WHERE destination_country contains "US" And origin_country IN ("CN", "HK") 

應該做你所需要的。我不知道這個版本是否運行正常,但它應該做你想要的過濾。

請參閱string function reference文檔中的SPLIT文檔以及有關nested and repeated fields的文檔。