2016-07-28 78 views
0

我必須執行下面的邏輯。我有一個有幾個字段的表格,從那張表格中我必須選擇具有c組但不是d的組。一些刺激物將有c和d一樣的組。從選定的刺激,我必須選擇其他領域,如經度,緯度等。如何從BigQuery表中選擇特定的記錄?

從下表中,使用SQL,我如何檢索突出顯示的行?

enter image description here

回答

1

我的理解是,「c」表示「我」,而「d」表示「f」
下面是這個。
它識別那些刺的是在「我」的組,但不是在「F」組

SELECT Prod 
FROM (
    SELECT Prod, 
    CONCAT('|', GROUP_CONCAT_UNQUOTED(group, '|'), '|') AS list 
    FROM YourTable 
    GROUP BY Prod 
) 
WHERE list CONTAINS '|i|' 
AND NOT list CONTAINS '|f|' 

您可以通過以下

SELECT Prod 
FROM (
    SELECT 
    Prod, 
    CONCAT('|', GROUP_CONCAT_UNQUOTED(group, '|'), '|') AS list 
    FROM 
    (SELECT 'prod1' AS Prod, 'i' AS group), 
    (SELECT 'prod1' AS Prod, 'f' AS group), 
    (SELECT 'prod1' AS Prod, 'f' AS group), 
    (SELECT 'prod2' AS Prod, 'i' AS group), 
    (SELECT 'prod3' AS Prod, 'i' AS group), 
    (SELECT 'prod4' AS Prod, 'i' AS group), 
    (SELECT 'prod4' AS Prod, 'f' AS group), 
    (SELECT 'prod4' AS Prod, 'f' AS group), 
    (SELECT 'prod4' AS Prod, 'f' AS group), 
    GROUP BY Prod 
) 
WHERE list CONTAINS '|i|' 
AND NOT list CONTAINS '|f|' 

,所以你最終的查詢能看起來像

測試這個
SELECT <list of needed fields> 
FROM YourTable 
WHERE Prod IN (
    SELECT Prod 
    FROM (
    SELECT Prod, 
     CONCAT('|', GROUP_CONCAT_UNQUOTED(group, '|'), '|') AS list 
    FROM YourTable 
    GROUP BY Prod 
) 
    WHERE list CONTAINS '|i|' 
    AND NOT list CONTAINS '|f|' 
) 
+0

abouve查詢是非常有用的,在構建我想要的。謝謝 – rbkk2016