SQL 意外选择

Posted

技术标签:

【中文标题】SQL 意外选择【英文标题】:SQL Unexpected SELECT 【发布时间】:2017-01-23 16:36:44 【问题描述】:

我在 C# 中根据传入的参数动态构造一个查询。它根据我传入的内容正确构建查询,但我无法弄清楚为什么查询不断抛出Unexpected SELECT 错误。错误指的是此查询中的最后一个选择,紧随 group 和 order by。

这是我的程序输出的查询:

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

【问题讨论】:

为什么不直接将查询粘贴到 Web UI 并让 BigQuery 为您检查语法? 【参考方案1】:

您在 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. 现在您可以使用代码折叠功能了。您可以折叠(折叠)代码片段,将它们减少为单个可见行。通过这种方式,您可以隐藏目前看来不重要的细节。如有必要,可以展开(展开)折叠的代码片段。折叠时 - 您可以观察您的代码,将实现细节放在一边,专注于结构等。它还有助于识别您的案例中的错误 4.然后可以使用编辑器的括号匹配功能(如下) 清楚地看到匹配的括号丢失

顺便说一句,BigQuery Mate 有一个名为 Show Outline 的功能,在很多情况下,当您有繁琐的查询并想获得高级别的“图片”时,它也可以提供帮助。它在 Mate 面板和上下文菜单中可用

【讨论】:

现在它抛出了一个Unrecognized name: activity at [7:13] 错误。 @JoeScotto - 1) 一次一个问题!看起来这个问题得到了回答! 2)在问下一个问题之前 - 真的 - 请先尝试解决您的问题。很抱歉这么说——但看起来你正在使用 SO 来外包你的工作。我们是来帮助你的,而不是做你的工作!不要把它当作个人 - 只是尽量遵守 SO 的规则 你在说什么,我没有投反对票。 仅供参考 - 我删除了上面的几个 cmets

以上是关于SQL 意外选择的主要内容,如果未能解决你的问题,请参考以下文章

SQL 编译错误:位置 6 处的语法错误第 2 行意外'R'

存储过程 Advantage Database SQL 中出现错误的意外令牌

PDO::FETCH_CLASSTYPE - 意外结果

sql 意外关闭Sql脚本

java.sql.SQLSyntaxErrorException:意外令牌:([关闭]

SQL 聚合总和产生意外输出