Exists - 指令碼函數
Exists() 會確定載入指令碼中的欄位內是否已載入特定欄位值。該函數會傳回 TRUE 或 FALSE,可用於 LOAD 陳述式或 IF 函數的 where 子句中。
語法:
Exists(field_name [, expr] )
傳回資料類型:布林
引數:
引數 | 描述 |
---|---|
field_name |
您想要搜尋值的欄位名稱。您可以使用沒有引號的明確欄位名稱。 必須已透過指令碼載入欄位。這表示,您無法參考透過子句、再透過指令碼載入的欄位。 |
expr |
您要檢查的值 (若存在)。您可以使用一個明確的值,或使用參考目前 LOAD 陳述式中一個或數個欄位的運算式。 資訊備註您無法參考未納入目前 LOAD 陳述式中的欄位。
此引數為選擇性。若您省略,函數將會檢查目前記錄中的 field_name 值是否已存在。 |
範例 1:
Exists (Employee)
如果目前記錄中欄位 Employee 的值已經存在於任何先前讀取的記錄 (包含該欄位) 中,則會傳回 -1 (True)。
範例 2:
Exists(Employee, 'Bill')
如果欄位值 'Bill' 被發現位於欄位 Employee 的目前內容中,則會傳回 -1 (True)。
陳述式 Exists (Employee, Employee) 與 Exists (Employee) 相當。
範例 3:
此結果位於資料模型中名為 EmployeeAddresses 的表格中,可以將其視為使用 Employee 和 Address 維度的表格圖表。
where 子句:where Exists (Employee, Name),僅表示來自表格 Citizens 的名稱,這些名稱也位於載入至新表格的 Employees 中。Drop 陳述式會移除暫時表格 Employees 和 Citizens,以避免混淆。
Employee | Address |
---|---|
Bill | New York |
John | Miami |
Steve | Chicago |
範例 4:
透過 where not Exists,在建置表格 EmployeeAddresses 的先前範例中,使用以下內容取代樣本資料中的陳述式。
NonEmployee:
Load Name as Employee, Address Resident Citizens where not Exists (Employee, Name);
where 子句包括 not:where not Exists (Employee, Name)。
這表示,只有來自不是位於 Employees 的表格 Citizens 的名稱會載入新的表格中。
請注意,Citizens 表格中有兩個 Lucy 的值,但只有一個值納入結果表格中。載入第一列時,值會納入 Employee 符號表格中。因此,檢查第二行時,值已經存在。
下一個範例顯示如何載入所有值。
Employee | Address |
---|---|
Mary | London |
Lucy | Madrid |
範例 5:
為了能夠取得 Lucy 的所有值,您需要變更兩件事:
將之前的載入新增至 Employees,在此您將 Employee 重新命名為 Name。
Load Employee As Name, ID, Salary;
將 Citizens 中的 Where 條件變更為:
not Exists (Name, Employee)。
這將會為 Name 和 Employee 建立不同的符號表格。檢查 Lucy 的第二列時,這仍然沒有存在於 Name。
Employee | Address |
---|---|
Mary | London |
Lucy | Madrid |
Lucy | Paris |
範例中使用的資料: