我的表结构看起来与此类似
Customer_id Country item_type Order_Size Dates Codes
A401 US Fruit Small 3/14/2016 11
A401 US Fruit Big 5/22/2016 12
A401 US Vegetable Small 7/12/2016 11
B509 US Vegetable Small 3/25/2015 92
B509 US Vegetable Big 3/15/2014 11
B509 US Vegetable Small 3/1/2014 34
A402 CA Fruit Small 3/14/2016 56
A402 CA Fruit Big 5/22/2016 76
A402 CA Fruit Small 7/12/2016 85
A403 CA Vegetable Small 7/12/2016 11
A403 CA Vegetable Small 3/25/2015 16
A403 CA Vegetable Big 3/15/2014 17
A403 CA Vegetable Small 3/1/2014 12
我正在寻找每个国家/地区只有在购买 Order_size =Big 并且仅使用 order_size<>Big 购买的商品后,才会出现每个 item_type 的重复客户数量。为了实现这一点,我编写了这段代码。
SELECT Country,item_type,count(customer_id) from
(select Country,customer_id, t.item_type, count(*) as REPEATS
from (select t.*,
min(case when Order_Size = 'Big' then dates end) over (partition by customer_id, item_type) as min_big
from data_test as t
) t
where dates > min_big
group by 1,2,3) D
group by 1,2
结果:
Country item_type Count(Distinct(Customer_id))
CA Vegetable 1
US Vegetable 1
CA Fruit 1
这现在有效,但我想再添加一个条件,仅当代码在某个带有条件的表中时,所以我想添加多个条件,其中一个是子查询,当我修改我的代码时。
SELECT Country,item_type,count(customer_id) from
(select Country,customer_id, t.item_type, count(*) as REPEATS
from (select t.*,
min(case when (Order_Size = 'Big' and Codes IN (SELECT CODES from table1 where type='TRUE' group by 1)) then dates end) over (partition by customer_id, item_type) as min_big
from data_test as t
) t
where dates > min_big
group by 1,2,3) D
group by 1,2
这会引发错误 - case 语句中的 When 子句中的非法表达式。我还读到您不能在 case 中使用子查询,也不能使用IN。我已经阅读了许多与此相关的其他问题,但我仍然不清楚如何避免使用子查询以防万一。如何更改不会引发错误并且由于我的表非常大而可以快速处理的代码?