2

我是 sql (以及任何类型的编码)的完整新手,但我正在尝试编写一个基本查询,该查询将按国家/地区返回 8 月 15 日之后首次登录的用户数量,以及在第一次会话后的第二天回来的用户数量。

我正在使用一个名为 events 的表,其中包含以下列:utc_timestamp、name、id 和一个包含此事件的几个参数的 json 字符串(如您所见,我正在使用它来检索会话编号和国家/地区)

当我运行此查询时,它显示“第 5 行:无法识别函数规范中 'SELECT' 'DISTINCT' 'id' 附近的输入”。我尝试在 select 和 DISTINCT 之间放置括号,但我仍然收到相同的错误消息。知道是什么原因造成的吗?

谢谢你们的帮助

SELECT 
get_json_object(json, '$. User_Country ') AS country
, COUNT(DISTINCT id) AS Users
, COUNT(
    SELECT DISTINCT id 
    FROM events 
    WHERE EXISTS(
        SELECT * 
        FROM events
        WHERE name = "Logged_in" 
        AND utc_timestamp>(
            (
            SELECT utc_timestamp
            FROM events
            WHERE month = 201408
            AND name = "Logged_in" 
            AND get_json_object(json, '$. Session_nb ') = 0
            AND utc_timestamp > UNIXTIMESTAMP('2014-08-15 12:00:00')
            ) + INTERVAL '1 day'
        )
    )
) AS Retained1
FROM events 
WHERE month = 201408 
AND name = "Logged_in" 
AND get_json_object(json, '$. Session_nb ') = 0
AND utc_timestamp > UNIXTIMESTAMP('2014-08-15 12:00:00')
GROUP BY (get_json_object(json, '$. User_Country ')) 
ORDER BY (get_json_object(json, '$. User_Country '))
4

1 回答 1

1

不要计算子查询结果。相反,COUNT在子查询本身中使用。

SELECT 
get_json_object(json, '$. User_Country ') AS country
, COUNT(DISTINCT id) AS Users
, (
    SELECT COUNT(DISTINCT id)
    FROM events 
    WHERE EXISTS(
        SELECT * 
        FROM events
        WHERE name = "Logged_in" 
        AND utc_timestamp>(
            (
            SELECT utc_timestamp
            FROM events
            WHERE month = 201408
            AND name = "Logged_in" 
            AND get_json_object(json, '$. Session_nb ') = 0
            AND utc_timestamp > UNIXTIMESTAMP('2014-08-15 12:00:00')
            ) + INTERVAL '1 day'
        )
    )
) AS Retained1
FROM events 
WHERE month = 201408 
AND name = "Logged_in" 
AND get_json_object(json, '$. Session_nb ') = 0
AND utc_timestamp > UNIXTIMESTAMP('2014-08-15 12:00:00')
GROUP BY (get_json_object(json, '$. User_Country ')) 
ORDER BY (get_json_object(json, '$. User_Country '))
于 2014-09-12T12:19:22.957 回答