简体   繁体   中英

SQL BigQuery - Error that variable is not grouped by even though it is

SQL Code:

 SELECT community_table.community_name,
        community_table.id,
        DATE(timestamp) as date,
        ifnull(COUNT(distinct app_opened.user_id), 0) as num_opened_DAU,
        lag(COUNT(distinct app_opened.user_id)) OVER 
        (ORDER BY community_table.community_name, community_table.id, DATE(timestamp)) as pre_Value
        
        FROM *** app_opened

        LEFT JOIN (
          SELECT DISTINCT id, community_id_2, context_traits_first_name, context_traits_last_name
          FROM (
                SELECT *
                FROM ***,
                UNNEST (JSON_EXTRACT_ARRAY(context_traits_community_ids, "$")) as community_id_2
               ) 
          GROUP by community_id_2, id, context_traits_first_name, context_traits_last_name) as community_id_table
        ON community_id_table.id = app_opened.user_id

        LEFT JOIN (
          SELECT DISTINCT id, name as community_name
          FROM ***) as community_table
        ON TO_JSON_STRING(community_table.id) = community_id_table.community_id_2

        WHERE app_opened.user_id is not null AND 
              EXTRACT(DAYOFWEEK FROM DATE(timestamp)) = 2 AND
              community_table.community_name is not null
        GROUP BY community_table.community_name, community_table.id, DATE(timestamp)

Error Message: 在此处输入图像描述

I am quite confused on what could be going wrong here, as the error says that timestamp is not grouped, even though I have grouped it at the bottom. I tried including just timestamp rather than Date(timestamp), but that ruins the table data that I am trying to create, where I find the number of users on a single day. Does anyone have any other ideas? My goal is for a single row, get the previous row's data, but because I am grouping by specific metrics, I need to make sure they are ordered by them as well. Thank you so much!

I think you simply need to modify OVER part as:

OVER (PARTITION BY community_table.community_name, community_table.id, DATE(timestamp)) as pre_Value

UPDATE. Seems that the problem was caused by using DATE() function within OVER so it can be solved by using DATE(timestamp) inside of subquery and passing alias to OVER

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM