簡體   English   中英

在SQL中計算一定數量的項目

[英]counting a certain amount of items in sql

我正在嘗試找到一種方法來計算可能手頭上有一定商品(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;

我想要的輸出將只是一個具有商店總數的列。 我只是真的不知道該怎么做才能提取所需的數據。 任何幫助是極大的贊賞。

我認為這是您要編寫的查詢:

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;

筆記:

  • 我不知道數量是做什么的。 我懷疑這導致查詢沒有任何匹配。 也許您想要<=代替。
  • 注意使用有意義的表別名(而不是ab )。
  • 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM