仅在满足两个条件时才能查找重复项目。在此示例中,仅当订单大小为“大”并且其对应日期在其他实例之前时,才计算每个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;
如何将日期方面包含在内?
答案 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
大案例陈述如下:
'Big'
,则忽略它customer_id
/ item_type
的每个组排序所有记录,并查看此记录之前的所有记录以及该组记录中的min(order_size)
(按字典顺序排序)是'Big'
然后你有一个大的前一个日期,可以统计这个记录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;