如何计算在不同产品之后销售某种产品的每个实例? SQL 或 DAX
How to count each instance where a certain product was sold after a different product? SQL or DAX
抱歉,如果标题看起来令人困惑,这是我能想到的最好的。
我可以同时使用 excel(Dax 因为它是一个强大的查询)和 sql:
我的情况是购买了两种产品类型,Type_A 和 Type_B。
我想计算在购买 "Type_B" 产品类型后,有多少唯一 Loc_ID 购买了 "Type_A" 产品类型。
根据我的示例,总共有 3 个唯一 Loc_ID 会落入此过滤器:Loc_01、Loc_02 和 Loc_04
非常感谢任何帮助
试试这个(如果每个 loc_id 都像您的示例一样购买两种类型的产品,效果很好。
select count(*)
from
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_a'
group by loc_id) a,
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_b'
group by loc_id) b
where a.loc_id=b.loc_id and a.dt>b.dt;
即使某些 loc_id
没有购买这两种类型的 products
,这仍然有效
试试这个:-
Select count(a.loc_id) as cnt_locations
from
your_table_name a
inner join
(
Select a.loc_id,b.date_purchased,b.Product_type
from
(
Select loc_id, min(date_purchased) as date_purchased
from
your_table_name
group by loc_id
) a
inner join
your_table_name b
on a.loc_id=b.loc_id and a.date_purchased =b.date_purchased
where Product_type ='Type_B'
) b
on
a.loc_id=b.loc_id
where a.date_purchased >b.date_purchased and a.Product_type ='Type_A'
抱歉,如果标题看起来令人困惑,这是我能想到的最好的。
我可以同时使用 excel(Dax 因为它是一个强大的查询)和 sql:
我的情况是购买了两种产品类型,Type_A 和 Type_B。
我想计算在购买 "Type_B" 产品类型后,有多少唯一 Loc_ID 购买了 "Type_A" 产品类型。
根据我的示例,总共有 3 个唯一 Loc_ID 会落入此过滤器:Loc_01、Loc_02 和 Loc_04
非常感谢任何帮助
试试这个(如果每个 loc_id 都像您的示例一样购买两种类型的产品,效果很好。
select count(*)
from
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_a'
group by loc_id) a,
(select loc_id , max(date_purchased) dt
from table t where product_type = 'type_b'
group by loc_id) b
where a.loc_id=b.loc_id and a.dt>b.dt;
即使某些 loc_id
没有购买这两种类型的 products
试试这个:-
Select count(a.loc_id) as cnt_locations
from
your_table_name a
inner join
(
Select a.loc_id,b.date_purchased,b.Product_type
from
(
Select loc_id, min(date_purchased) as date_purchased
from
your_table_name
group by loc_id
) a
inner join
your_table_name b
on a.loc_id=b.loc_id and a.date_purchased =b.date_purchased
where Product_type ='Type_B'
) b
on
a.loc_id=b.loc_id
where a.date_purchased >b.date_purchased and a.Product_type ='Type_A'