汇总表查询

时间:2017-11-06 00:06:27

标签: sql teradata

我有以下查询:

SELECT territory,
       ( CASE
             WHEN content_type_cat IN ('A',
                                       'AB',
                                       'AC') THEN 'ABC'
             ELSE 'XYZ'
         END ) AS content_type,
       count(DISTINCT customerid) AS unique_customers
FROM table.table1
WHERE date BETWEEN '2016-05-09' AND '2016-07-18'
  AND content_type_cat IN ('A',
                           'AB',
                           'AC',
                           'D',
                           'E')
  AND one_more_column NOT IN ('alpha',
                              'beta',
                              'gamma')
GROUP BY 1,
         2;

是否可以帮我修改此查询以获取以下报告:

date| Territory| content_type|no_unique_customers_30_days

4 个答案:

答案 0 :(得分:2)

根据您想要的范围(通常称为“条件聚合”)在聚合函数中使用案例表达式

SELECT
      territory
    , (
      CASE
            WHEN content_type_cat IN ('A', 'AB', 'AC') THEN 'ABC'
            ELSE 'XYZ'
      END
      ) AS content_type
    , COUNT(DISTINCT
               CASE
                   WHEN date >= current_date - 30 
                    AND date < current_date THEN customerid
               END
      ) AS unique_customers_30_days
FROM table1
WHERE date BETWEEN '2016-05-09' AND '2016-07-18'
AND content_type_cat IN ('A', 'AB', 'AC', 'D', 'E')
AND one_more_column NOT IN ('alpha', 'beta', 'gamma')
GROUP BY 1 , 2;

答案 1 :(得分:0)

SELECT MAX(Date),   --MIN(DATE) 
      territory,
     (
      CASE
            WHEN content_type_cat IN ('A', 'AB', 'AC') THEN 'ABC'
                 ELSE 'XYZ'
      END
      ) AS content_type,
     COUNT(DISTINCT
               CASE
                   WHEN date >= current_date - 30 
                    AND date < current_date THEN customerid
               END
      ) AS unique_customers_30_days
FROM table1
WHERE date BETWEEN '2016-05-09' AND '2016-07-18'
AND content_type_cat IN ('A', 'AB', 'AC', 'D', 'E')
AND one_more_column NOT IN ('alpha', 'beta', 'gamma')
GROUP BY 2,3;

答案 2 :(得分:0)

一种可能的解决方案是基于非等自连接,这可能会在聚合之前创建一个大的中间结果。如果行数很小,它仍然有效:

WITH cte AS
 ( 
   SELECT DISTINCT territory,
       ( CASE
             WHEN content_type_cat IN ('A',
                                       'AB',
                                       'AC') THEN 'ABC'
             ELSE 'XYZ'
         END ) AS content_type,
         customerid
   FROM TABLE.table1
   WHERE content_type_cat IN ('A',
                              'AB',
                              'AC',
                              'D',
                              'E')
   AND one_more_column NOT IN ('alpha',
                               'beta',
                               'gamma')
 )  
SELECT t1.territory,
       t1.content_type,
       Count(DISTINCT t2.customerid) AS unique_customers
FROM cte AS t1
JOIN cte AS t2
  ON t2.territory = t1.territory
 AND t2.content_type = t1.content_type
 -- returns all matching row in the 30 day time frame
 AND t2.datecol BETWEEN t1.datecol-29 AND t1.datecol 
GROUP BY 1,
         2;

答案 3 :(得分:0)

我认为你需要这样的东西:

select
    enddate,
    startdate,
    t.territory,
    content_type,
    min(t.date) as min_date,
    max(t.date) as max_date,
    count(distinct t.customerid) as count_customer
from
    (select
      date as endDate,
      date-30 as startDate,
      content_type_cat,
      territory,
      (CASE 
         WHEN content_type_cat IN ('A','AB','AC') THEN 'ABC' 
         ELSE 'XYZ' -- content_type_cat IN ('D','E')
       END ) AS content_type,
      customerid
    from
      db.table
    where
      date BETWEEN '2016-05-09' AND '2016-07-18'
      AND content_type_cat IN ('A','AB','AC','D','E')
      AND one_more_column NOT IN ('alpha', 'beta', 'gamma')  ) d inner join
    (select * 
     from
       db.table
     where
      date BETWEEN '2016-05-09' - 30 AND '2016-07-18'
      AND content_type_cat IN ('A','AB','AC','D','E')
      AND one_more_column NOT IN ('alpha', 'beta', 'gamma') ) t on
    t.date >= startdate and
    t.date <= enddate and
    t.territory = d.territory and
    t.content_type_cat = d.content_type_cat and
    t.customerid = d.customerid -- and join more columns?  not sure how to identify rows you want counted
where
    endDate BETWEEN '2016-05-09' AND '2016-07-18' 
group by
    1,2,3,4

但您确实需要了解要用作结束日期的行与30天前相关的行集之间的关系。

相关问题