SQL Select Count with join on other table不包含0计数

时间:2013-12-28 09:52:56

标签: sql sql-server-2008 count

我在SQL Server 2008上有以下数据库:

用户表:

    id           Name            Country      BloodTypeId   
---------+---------------+------------+-----------------+
1            John            US            1            
2            Kate            China         1
3            Sam             US            2
4            Tom             Canada        1
5            Mike            US            1            
6            Carol           China         3
7            Daniel          US            2
8            Joseph          US            1
9            Mary            US            3            
10           Peter           China         1

Bloodtypes表

    BloodTypeId      BloodTypename    
------------+------------------+
1                 A
2                 B
3                 O
4                 AB

我需要一个sql脚本,它将返回每个国家/地区的血型数量: 所以对于上面的数据库,我需要它返回:

    Country      BloodType     Count
---------+---------------+------------+
US               A          3
US               B          2
US               O          1
US               AB         0
China            A          2  
China            B          0
China            O          1
China            AB         0
Canada           A          1
Canada           B          0
Canada           O          0
Canada           AB         0

请注意,如果某个国家/地区没有针对特定血型的用户,我需要将其显示为0计数。

我尝试了什么: 我尝试了以下脚本:

SELECT Country, BloodTypename as BloodType, COUNT(1) as Count 
FROM Users
JOIN Bloodtypes
ON Users.BloodtypeId = Bloodtypes.BloodtypeId 
GROUP BY  Country, BloodTypename
ORDER BY Country

此查询给出了我的结果:

     Country      BloodType     Count
---------+---------------+------------+
US               A          3
US               B          2
US               O          1
China            A          2  
China            O          1
Canada           A          1

0计数的血型未出现

我尝试使用RIGHT JOIN代替:

SELECT Country, BloodTypename as BloodType, COUNT(1) as Count 
FROM Users
RIGHT JOIN Bloodtypes
ON Users.BloodtypeId = Bloodtypes.BloodtypeId 
GROUP BY  Country, BloodTypename
ORDER BY Country

但仍然得到相同的结果

如何选择我需要的东西并包括所有血型,即使是0计数的血型?

非常感谢

2 个答案:

答案 0 :(得分:0)

尝试使用left outer join它返回左表(Users)中的所有行,右表中的匹配行(血型)。当没有匹配时,结果在右侧为NULL,因此NULL的计数将为0

SELECT Country, BloodTypename as BloodType, COUNT(1) as Count 
FROM Users LEFT OUTER
JOIN Bloodtypes
ON Users.BloodtypeId = Bloodtypes.BloodtypeId 
GROUP BY  Country, BloodTypename
ORDER BY Country

答案 1 :(得分:0)

您必须使用country然后使用CROSS JOIN获取笛卡尔积的所有组合LEFT JOIN和血型。像这样:

SELECT 
  t.country,
  t.BloodTypename,
  COUNT(DISTINCT u.id) as Count 
FROM
(
  SELECT u.country, b.BloodTypeId, b.BloodTypename
  FROM Users u 
  CROSS JOIN Bloodtypes b
)AS t
LEFT JOIN Users AS u ON u.BloodtypeId = t.BloodtypeId  AND t.country = u.country
GROUP BY t.country,  t.BloodTypename
ORDER BY Country DESC;

这会给你:

| COUNTRY | BLOODTYPENAME | COUNT |
|---------|---------------|-------|
|      US |             A |     3 |
|      US |            AB |     0 |
|      US |             B |     2 |
|      US |             O |     1 |
|   China |             A |     2 |
|   China |            AB |     0 |
|   China |             B |     0 |
|   China |             O |     1 |
|  Canada |             A |     1 |
|  Canada |            AB |     0 |
|  Canada |             B |     0 |
|  Canada |             O |     0 |