使用count来计算整数列的值集

时间:2015-02-10 05:17:38

标签: sql

我对SQL世界还很陌生。我想知道基于location_id的每个位置有多少商店。我知道有必要计算数量。当我运行下面的查询时,由于错误而没有结果?

SELECT COUNT(location_id) FROM stores

表:

stores

ID Location_id Store_Name
-- ----------- ----------
1      1         Example1
2      1         Example2
3      2         Example3
4      2         Example4

3 个答案:

答案 0 :(得分:2)

select  Location_id,count(Store_Name)
from stores
group by Location_id

此查询将在每个位置的该位置返回Location_Id后跟number of stores

答案 1 :(得分:1)

您必须按位置对记录进行分组并对其进行计数。您可以将大多数aggregate functions与group by语句一起使用。

 SELECT LOCATION_ID, COUNT(*) FROM stores GROUP BY LOCATION_ID

答案 2 :(得分:0)

SELECT Location_id, COUNT(Store_Name) as count FROM stores
group by Location_id