我是SQL的新手,我想知道是否有人可以帮助我的代码。
我正在尝试计算绑定到某个仓库的商店的不同数量,该仓库绑定到一个采购订单。
例如:如果有100家商店具有来自2号仓库或5号仓库或其他仓库的PO。那么我想:
| COUNT_STORE | WH_LOCATION |
1 | 100 | 2 |
2 | 25 | 5 |
3 | 56 | 1 |[]我的代码:
select count(distinct Store_ID) as Count_Store, WH_Location
from alc_Loc
where alloc_PO = 11345
group by Store_ID, WH_Location当我运行这个命令时,"count_store“会得到一个1,它会多次向我显示WH_Location。我觉得好像有什么不对劲。
如有任何帮助,我们不胜感激!
发布于 2018-01-12 22:49:22
只需从group by中删除store_id
select count(distinct Store_ID) as Count_Store, WH_Location
from alc_Loc
where alloc_PO = 11345
group by WH_Location;当您在group by中包含Store_ID时,每个Store_ID都有一个单独的行。因此,distinct计数显然是1(如果存储id是NULL,则为0)。
https://stackoverflow.com/questions/48228613
复制相似问题