SQL QUERY用于计算具有2个条件的重复

时间:2017-03-31 17:22:24

标签: sql teradata

eg table schema and required output

仅在满足两个条件时才能查找重复项目。在此示例中,仅当订单大小为“大”并且其对应日期在其他实例之前时,才计算每个customer_id的项类型的重复次数。第一个条件和重复可以通过使用此代码来实现。

Select Customer_id, Item_Type, COUNT(*) 
from table 
group by Customer_id, Item_Type
having count(*) > 1 and sum(case when Order_Size = 'Big' then 1 else 0 end) > 0; 

如何将日期方面包含在内?

2 个答案:

答案 0 :(得分:0)

我相信你可以在子查询中使用窗口函数来决定要计算哪些行,然后在主查询中计算它们。类似的东西:

Select
    customer_id, item_type, sum(count_pass) as Count
FROM
    (
        Select Customer_id, 
            Item_Type, 
            CASE 
                WHEN Order_Size = 'Big' THEN 0 
                WHEN MIN(Order_Size) OVER (PARTITION BY Customer_ID, Item_Type ORDER BY DateField ASC ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING) = 'BIG' THEN 1 
                ELSE 0
                END as count_pass
        FROM table
    ) subqry
GROUP BY 1,2

大案例陈述如下:

  1. 如果此记录为'Big',则忽略它
  2. 如果按日期为customer_id / item_type的每个组排序所有记录,并查看此记录之前的所有记录以及该组记录中的min(order_size)(按字典顺序排序)是'Big'然后你有一个大的前一个日期,可以统计这个记录
  3. 否则......你无法指望它。哪个只是order_size = 'small'而没有前一个'big'的记录。

答案 1 :(得分:0)

我会这样做:

select t.customer_id, t.item_type, count(*)
from (select t.*,
             min(case when OrderSize = 'Big' then date end) over (partition by customer_id, item_type) as min_big
      from t
     ) t
where date > min_big
group by t.customer_id, t.item_type;