根据过滤器计算记录

时间:2014-09-16 13:41:17

标签: sql tsql stored-procedures

我在sql中有4个表,其中包含一些记录。

Users

UserId  UserName  EmploymentId
------  --------  -------------
1       User1      1          
2       John       1          
3       Doe        2   

Country

CountryId  countryName  
------     --------  
1          USA                
2          UK           

EmpStatus

EmploymentId  EmpName  
------        --------  
1             Employed                
2             Un-Employed           

UserNationality (many to many )

UserId  CountryId
------  --------  
1          1          
1          1          
3          1   

现在我想要实现的是使用过滤条件获得显示计数.i.e。我有查看哪个应该显示过滤条件,如下所示。

**Search Records**

Search by country

USA (3)
UK  (0)

Search by employment

Employed (2)
Un-Employed (1)

请注意,在UserNationality表格中,UserIdCountryId之间存在多对多关系。因此,可能会或可能不会有针对用户的记录。

我已尝试通过在表格上应用连接进行计数,但它没有给出所需的结果。

还尝试使用PARTITION但我到目前为止未能生成结果。我应该用什么方法来解决这个问题?

0 个答案:

没有答案
相关问题