Id Name City
1 Hits Baroda
2 Ajay Chennai
3 Hitesh Baroda
如何计算城市以及如何将其计数值存储在变量中?
Id Name City
1 Hits Baroda
2 Ajay Chennai
3 Hitesh Baroda
如何计算城市以及如何将其计数值存储在变量中?
如果您只想要城市的总数,请尝试查询
SELECT COUNT(*) as TotalCityCount From TableName
如果您只想要城市的总数,请删除重复的城市
SELECT COUNT(distinct City) as TotalCityCount From Your TableName
并尝试此 c# 代码:
public DataView GetCityCount()
{
using (SqlConnection con = new SqlConnection("Put Your Connection String"))// **must Put Your Connection String**
{
string sql1 = string.Format(@"SELECT COUNT(*) as TotalCityCount From TableName");
SqlDataAdapter da1 = new SqlDataAdapter(sql1, con);
DataSet ds1 = new DataSet();
con.Open();
da1.Fill(ds1);
return ds1.Tables[0].DefaultView;
}
}
Public Void getTotal()
{
DataView dv=GetCityCount();
int totalcity=Convert.ToInt32(dv.Tables[0]["TotalCityCount"])//You get the total Count value in this totalcity variable
}
这将为您提供每个城市的人数,在这里您可以获得城市人数
SELECT
City,
COUNT(Id) as total
From
TableName
Group by
City
结果将是
Baroda 2
Chennai 1
或者,如果您只想要城市总数,请这样写
SELECT
COUNT(distinct City) as total
From
TableName
可能是你需要的这些
declare @Count int --(or bigint)
Select @Count=count(city)
from Citytable
return @Count (or print @Count or Select @Count)