2017-11-21 2 views
0

Hier ist die erste AbfrageJoin zwei einfache Abfragen in BigQuery

SELECT 
    MONTH(TIMESTAMP(REGEXP_EXTRACT(date, '.*.([0-9]{4})$') + '-' + 
    REGEXP_EXTRACT(date, '.([0-9]{2}).') + '-' + 
    REGEXP_EXTRACT(date, '^([0-9]{2}).*'))) AS month, 
    COUNT(DISTINCT cons_id) 
FROM 
    [table1] 
WHERE 
    dbo_type = 'smth' 
GROUP BY month 

nach Monat gruppiert Benutzer-IDs zurück. Hier ist der zweite ein

SELECT 
    MONTH(TIMESTAMP(REGEXP_EXTRACT(date, '.*.([0-9]{4})$') + '-' + 
    REGEXP_EXTRACT(date, '.([0-9]{2}).') + '-' + 
    REGEXP_EXTRACT(date, '^([0-9]{2}).*'))) AS month, 
    COUNT(DISTINCT cons_id) 
FROM 
    [table1] 
WHERE 
    dbo_type = 'smth' 
    AND success_operations > 0 
GROUP BY month 

aktiv Benutzer-IDs gruppiert nach demselben Monate zurück. Wie trete sie um einen einfachen Tisch zu bekommen wie

 
month | users  | active_users 
------| --------- | --------- 
9  | 100  | 50 
10 | 120  | 60 
11 | 140  | 70 

--- ANTWORT ----

Danke, Michael!

#legacySQL 
SELECT 
    INTEGER(REGEXP_EXTRACT(DATE, '.([0-9]{2}).')) AS month, 
    EXACT_COUNT_DISTINCT(cons_id) AS users, 
    EXACT_COUNT_DISTINCT(IF(success_operations > 0, cons_id, NULL)) AS active_users 
FROM 
    [project:dataset.table] 
WHERE 
    dbo_type = 'smth' 
GROUP BY month 
ORDER BY month 

Antwort

1

Versuchen Sie unten für BigQuery Legacy-SQL (wie sieht aus wie Sie es in Ihrer Frage verwenden)

#legacySQL 
SELECT 
    MONTH(TIMESTAMP(REGEXP_EXTRACT(date, '.*.([0-9]{4})$') + '-' + 
    REGEXP_EXTRACT(date, '.([0-9]{2}).') + '-' + 
    REGEXP_EXTRACT(date, '^([0-9]{2}).*'))) AS month, 
    COUNT(DISTINCT cons_id) AS users, 
    COUNT(DISTINCT IF(success_operations > 0, cons_id, NULL)) AS active_users 
FROM 
    [project:dataset.table1] 
WHERE 
    dbo_type = 'smth' 
GROUP BY month 

Bitte beachten Sie: COUNT (DISTINCT) in Legacy-SQL ungefähre wird - mehr Details - https://cloud.google.com/bigquery/docs/reference/legacy-sql#countdistinct

können Sie verwenden EXACT_COUNT_DISTINCT statt

auch sieht es so aus t o mir, dass statt langwieriger

MONTH(TIMESTAMP(REGEXP_EXTRACT(date, '.*.([0-9]{4})$') + '-' + 
    REGEXP_EXTRACT(date, '.([0-9]{2}).') + '-' + 
    REGEXP_EXTRACT(date, '^([0-9]{2}).*'))) AS month 

können Sie einfach

INTEGER(REGEXP_EXTRACT(DATE, '.([0-9]{2}).')) 

Für BigQuery Standard-SQL verwenden, können Sie unten probieren:

#standardSQL 
SELECT 
    REGEXP_EXTRACT(DATE, '.([0-9]{2}).') AS month, 
    COUNT(DISTINCT cons_id) AS users, 
    COUNT(DISTINCT IF(success_operations > 0, cons_id, NULL)) AS active_users 
FROM `project.dataset.yourTable` 
WHERE dbo_type = 'smth' 
GROUP BY month 

Sie können damit testen/spielen Dummy-Daten unter Verwendung von wie unten

#standardSQL 
WITH `project.dataset.yourTable` AS (
    SELECT '31-12-2017' DATE, 1 cons_id, 1 success_operations, 'smth' dbo_type UNION ALL 
    SELECT '31-12-2017', 2, 0, 'smth' UNION ALL 
    SELECT '31-12-2017', 3, 0, 'smth' 
) 
SELECT 
    REGEXP_EXTRACT(DATE, '.([0-9]{2}).') AS month, 
    COUNT(DISTINCT cons_id) AS users, 
    COUNT(DISTINCT IF(success_operations > 0, cons_id, NULL)) AS active_users 
FROM `project.dataset.yourTable` 
WHERE dbo_type = 'smth' 
GROUP BY month 
+0

Danke für die Beantwortung. Die Aggregatfunktion MONTH funktioniert nur für Legacy-SQL. Sowie REGEXP_EXTRACT –

+0

oh, ich vermisste dies - konzentrierte sich auf wie zu zählen - wird diese kurz antworten. Bitte klären Sie - brauchen Sie es in Legacy oder Standard? Können Sie ein Beispiel für Ihr Datumsfeld angeben? –

+0

siehe Update meiner Antwort –