SQL意外的SELECT

时间:2017-01-23 16:36:45

标签: sql google-bigquery

我基于传入的参数在C#中动态构建查询。它根据我传入的内容正确构建查询但我无法弄清楚为什么查询会一直抛出意外的SELECT错误。错误是指此查询中的最后一个选择,紧跟在组和顺序之后。 这是我的程序输出的查询: with allTables as(     选择         UNIX_SECONDS(TIMESTAMP(DATE))作为日期,         SUM(CASE WHEN period = 7 THEN users END)AS days_07     FROM(         选择             activity.date AS DATE,             periods.period AS期间,             COUNT(DISTINCT用户)AS用户         FROM(             选择                 activity.date AS DATE,                 periods.period AS期间,                 COUNT(DISTINCT用户)AS用户             FROM(                 选择                     DISTINCT DATE(TIMESTAMP_MICROS(event.timestamp_micros))AS DATE,                     user_dim.app_info.app_instance_id AS用户                 来自`hidden。*`                 交叉加入                     UNNEST(event_dim)AS事件             )AS活动             交叉加入(                 选择                     DATE(TIMESTAMP_MICROS(event.timestamp_micros))AS DATE                 来自`hidden。*`                 交叉加入                     UNNEST(event_dim)AS事件                 GROUP BY event.timestamp_micros             )AS日期             交叉加入(                 SELECT period FROM(SELECT 7 AS period)             )AS期间             哪里                 dates.date> = activity.date             和                 SAFE_CAST(FLOOR(DATE_DIFF(dates.date,activity.date,DAY)/periods.period)AS INT64)= 0             GROUP BY 1,2         按日期排序按日期排序     )SELECT'所有应用'作为target,date as datapoint_time,days_07 as datapoint_value FROM allTables

1 个答案:

答案 0 :(得分:3)

在SELECT
之前,您还缺少一个结束括号 应该是

WITH allTables AS (
    SELECT 
        UNIX_SECONDS(TIMESTAMP(DATE)) AS DATE, 
        SUM(CASE WHEN period = 7  THEN users END) AS days_07 
    FROM (
        SELECT
            activity.date AS DATE,
            periods.period AS period,
            COUNT(DISTINCT user) AS users                                                   
        FROM ( 
            SELECT 
                activity.date AS DATE, 
                periods.period AS period, 
                COUNT(DISTINCT user) AS users 
            FROM ( 
                SELECT
                    DISTINCT DATE(TIMESTAMP_MICROS(event.timestamp_micros)) AS DATE,
                    user_dim.app_info.app_instance_id AS user 
                FROM `hidden.*` 
                CROSS JOIN 
                    UNNEST(event_dim) AS event 
            ) AS activity 
            CROSS JOIN ( 
                SELECT 
                    DATE(TIMESTAMP_MICROS(event.timestamp_micros)) AS DATE 
                FROM `hidden.*` 
                CROSS JOIN 
                    UNNEST(event_dim) AS event 
                GROUP BY event.timestamp_micros 
            ) AS dates 
            CROSS JOIN ( 
                SELECT period FROM (SELECT 7 AS period) 
            ) AS periods
            WHERE 
                dates.date >= activity.date 
            AND 
                SAFE_CAST(FLOOR(DATE_DIFF(dates.date, activity.date, DAY)/periods.period) AS INT64) = 0 
            GROUP BY 1,2 
        ) GROUP BY DATE ORDER BY DATE
    )
) SELECT 'all apps' AS target, DATE AS datapoint_time, days_07 AS datapoint_value FROM allTables  

希望分享处理此类问题的超级简单方法"
1.将疑问查询复制到查询编辑器
2.确保选择正确的SQL版本(可选但最好完成它)
3.现在您可以使用代码折叠功能。您可以折叠(折叠)代码片段,将它们缩小为单个可见行。通过这种方式,您可以隐藏目前看起来不重要的细节。如果需要,可以扩展(展开)折叠的代码片段。折叠时 - 您可以观察您的代码,将实现细节放在一边,并专注于结构等。此外,它还有助于识别您的案例中的错误 enter image description here
4.然后你可以使用编辑器的括号匹配功能(如下)
enter image description here
清楚地看到匹配的支架丢失了 enter image description here

Btw,BigQuery Mate有一个名为Show Outline的功能,当你有浓密的查询需要高级别的图片时,它也可以提供很多帮助。它可以在配合面板和上下文菜单中使用

enter image description here