我有一张表格,其中包含国家和城市的所有员工数据
主要表Tbl_Emp
ID Name Dept CountryID CityID
1 Arun Bikas Das IT 1 1
2 Abhineet Prakash IT 1 1
3 Avnish Dhoundiyal HR 2 5
4 Amit Batra HR 3 6
5 Ajay Sethi AC 3 6
6 Amarendra Pandey AC 3 6
7 Abhijit Singh ADM 1 3
8 Anil Kumar ADM 1 4
9 Mahesh Venu MAR 1 7
10 Mohamed Rafi MAR 1 9
Tbl_County
是
C_ID Country
1 India
2 USA
3 UK
tbl_city
是
C_ID City CountryID
1 Delhi 1
2 Delhi 1
3 Mumbai 1
4 Nagpur 1
5 NewYork 2
6 Caneda 3
7 Jaipur 1
9 Panjab 1
我想按县选择员工人数:
Country Employees
------- ---------
India 6
USA 1
UK 3
,同样适用于城市:
City Employees
------- ---------
Delhi 2
Mumbai 1
NewYork 1
Canada 3
Nagpur 1
Jaipur 1
Panjab 1
答案 0 :(得分:1)
为城市做同样的事情。请在发布之前再做一些搜索,这是一件很常见的事情,你应该找到很多例子。这样做有助于减少发布的低质量问题的数量。
SELECT C.Country, COUNT(*) AS CountryCount
FROM Tbl_Emp E
INNER JOIN TblCounty C
ON E.CountryID = C.C_ID
GROUP BY C.Country
答案 1 :(得分:1)
国家计数:
select
min(c.Country),
count(e.CountryID) as Employees
from
Tbl_Emp e
join Tbl_County c on e.CountryID = c.c_id
group by e.CountryID
对于城市数量:
select
min(c.city),
count(e.CityID) as Employees
from
Tbl_Emp e
join tbl_city c on e.CityID = c.c_id
group by e.CityID
这可以是另一种解决方案:
select
c.Country,
e.Employees
from (
select
CountryID, count(CountryID) as Employees
from Tbl_Emp
group by CountryID
) as e
join Tbl_County c on e.CountryID = c.c_id
select
c.city,
e.Employees
from (
select
CityID, count(CityID) as Employees
from Tbl_Emp
group by CityID
) as e
join tbl_city c on e.CityID = c.c_id
答案 2 :(得分:0)
COUNT(*)
函数返回表中的记录数:
SELECT COUNT(*) FROM table_name;
以下查询将根据国家/地区为您计算:
SELECT COUNT(*) AS Count, Tbl_Country.Country
FROM Tbl_Country INNER JOIN
Tbl_Emp ON Tbl_Country.C_ID = Tbl_Emp.CountryId
GROUP BY Tbl_Country.Country
尝试使用City查询,语法与上面相同