对于给定的查询,我需要聚合它 date(dt) ,目录级别,但我无法这样做。我尝试了以下查询,但结果为空白

Posted

技术标签:

【中文标题】对于给定的查询,我需要聚合它 date(dt) ,目录级别,但我无法这样做。我尝试了以下查询,但结果为空白【英文标题】:For the given query, I need to aggregate it date(dt) ,catalog level but I am unable to do so. I tried the following query but it gave blank result 【发布时间】:2021-06-12 08:52:32 【问题描述】:

我需要在日期、目录 ID、标志、来源级别聚合的数据,我需要使用数据过滤器过滤数据

select  CAST( catalog_id as int) as catalog_id
, dt
, CASE
    WHEN date_diff ('day',DATE (users.created), dt) <= 28 
      THEN 'New'
    ELSE 'Old'
  END AS flag
, origin
, count(user_id) as total_views
, count(distinct user_id) as unique_views
from
                     
        (select date(timestamp) as dt, catalog_id, user_id,
        -- case when origin = 'main' and screen='main' then 'FY_feed' 
        case when origin = 'main' then 'FY_feed' 
        when (origin = 'collections' )  OR (origin = 'single_collection')  then 'collections' 
        when origin = 'catalog_search_results' then 'search' 
        -- when origin = 'main' and screen = 'catalogs_filter' then 'catalogs_filter'
        -- when origin = 'notification' and screen = 'main' then 'notification' 
        -- when origin = 'notification' and screen = 'single_collection' then 'collection_notification' 
        when origin = 'notification' then 'notification' 
        when origin = 'banner'  then 'banner' 
        when (origin = 'account' ) OR (origin = 'my_shared_catalogs' )  then 'my_shared_catalogs'
         when origin= 'widget'  then 'widget' 
         when origin= 'wishlist' then 'wishlist'
         when origin= 'visual_search_results' then 'visual_search_results' 
         
        else 'Others' end as origin
        
        from catalog_views_report_final 
        where 
        date(timestamp) >= current_date - 3 
        and date(timestamp) <= current_date
        ) v 
        
LEFT join users on users.id = v.user_id
group by 
dt,
CASE
  WHEN date_diff ('day',DATE (users.created), dt) <= 28
    THEN 'New'
  ELSE 'Old'
END
, CAST( catalog_id as int)
, origin
LIMIT 10

【问题讨论】:

请提供样本数据和期望的输出,同时告诉我们你现在得到的结果/ 【参考方案1】:
select  CAST(catalog_id as int) as catalogs, dt, CASE
               WHEN date_diff ('day',DATE (users.created), dt) <= 28 THEN 'New'
               ELSE 'Old'
             END AS flag , origin, count(user_id) as total_views, count(distinct user_id) as unique_views (select date(timestamp) as dt, catalog_id, user_id,
-- case when origin = 'main' and screen='main' then 'FY_feed' 
case when origin = 'main' then 'FY_feed' 
when (origin = 'collections' )  OR (origin = 'single_collection')  then 'collections' 
when origin = 'catalog_search_results' then 'search' 
-- when origin = 'main' and screen = 'catalogs_filter' then 'catalogs_filter'
-- when origin = 'notification' and screen = 'main' then 'notification' 
-- when origin = 'notification' and screen = 'single_collection' then 'collection_notification' 
when origin = 'notification' then 'notification' 
when origin = 'banner'  then 'banner' 
when (origin = 'account' ) OR (origin = 'my_shared_catalogs' )  then 'my_shared_catalogs'
 when origin= 'widget'  then 'widget' 
 when origin= 'wishlist' then 'wishlist'
 when origin= 'visual_search_results' then 'visual_search_results' 
 
else 'Others' end as origin

from catalog_views_report_final 
where 
date(timestamp)='2021-06-08'
) v 

LEFT join users on users.id = v.user_id

group by 
1,2,3,4)



group by 1,2

【讨论】:

以上是关于对于给定的查询,我需要聚合它 date(dt) ,目录级别,但我无法这样做。我尝试了以下查询,但结果为空白的主要内容,如果未能解决你的问题,请参考以下文章

Django 使用表达式聚合查询

我如何需要对这个查询进行 GROUP 才能聚合 MAX

如何在查询中编写单个聚合

MongoDB聚合日期查询

MySQL 服务器上非常简单的 AVG() 聚合查询需要很长时间

SQL SERVER开窗函数