我有以下数据库:
股票(StockNo,商店代码,描述,数量,单位,重新排序, 价格,SuppCode)
这是其中包含的数据样本:
STOCKNO STOREC DESCRIPTION QUANTITY UNITS REORDER PRICE SUPPCO
------- ------ ----------------------- -------- ------ ------- ------- ------
126 LEG Sealing wax 9 Box 5 7.99 S5
127 LEG Red binding ribbon 13 Roll 10 6.47 S5
128 LEG A3 cream notary paper 21 Ream 10 7.85 S5
129 LEG Coloured ink 22 Bottle 10 3.48 S4
我需要进行查询以显示供应至少3家商店的供应商(suppcode)。
这是我到目前为止所拥有的:
select suppcode, count(distinct storecode) as StoresSupplied
from stocks
group by suppcode
having count(*) > 3
这会产生以下结果
SUPPCO STORESSUPPLIED
------ --------------
S3 4
S4 3
S2 3
S1 1
检查整个表格S3,S4和S2的结果是否正确,但我找不到S1的原因。 S1出现在桌面上,提供8个不同的商品,但只有1个单独的商店代码。
答案 0 :(得分:1)
Count(*)
将包含Null
行,而count(distinct storecode)
则不会。尝试改变这样
select suppcode, count(distinct storecode) as StoresSupplied
from stocks
group by suppcode
having count(distinct storecode) > 3
答案 1 :(得分:0)
我建议你只使用select
:
select suppcode, count(distinct storecode) as StoresSupplied
from stocks
group by suppcode
having StoresSupplied > 3;