我正试图找到一种方法来计算可能有某个项目(sku)的商店数量。我一直得到0作为我的输出,但我知道这不是真的,因为这个项目刚刚发布到某些商店。我已经尝试过没有DISTINCT关键字,它仍然无法正常工作。 表w_store具有sku和存储位置,表alc_loc也具有给该存储和sku的数量。
select distinct count(*) as total_stores
from(
select distinct a.store
from w_store a
join alc_loc b
on
b.item = a.sku
where a.sku = 1001
and b.cal_qty = b.all_qty
group by a.store, a.sku
having (a.sku) = 1
)a;
我想要的输出只是一个只有商店总数的列。我真的不知道如何处理我需要的数据。 任何帮助是极大的赞赏。
答案 0 :(得分:0)
我认为这是您要写的查询:
select count(*) as total_stores
from (select s.store
from w_store s join
alc_loc l
on l.item = s.sku and l.cal_qty = s.all_qty
where s.sku = 1001
group by s.store
having count(*) = 1
) sl;
注意:
<=
代替。a
和b
)。select distinct count(*)
没有意义。您真正想要的查询可能看起来更像这样:
select count(distinct s.store) as total_stores
from w_store s join
alc_loc l
on l.item = s.sku
where s.sku = 1001 and l.cal_qty > 0;