Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
其他表上具有联接的SQL Select计数不包括0计数_Sql_Sql Server 2008_Count - Fatal编程技术网

其他表上具有联接的SQL Select计数不包括0计数

其他表上具有联接的SQL Select计数不包括0计数,sql,sql-server-2008,count,Sql,Sql Server 2008,Count,我在SQL Server 2008上有以下数据库: 用户表: id Name Country BloodTypeId ---------+---------------+------------+-----------------+ 1 John US 1 2 Kate China

我在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
血型表

    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的血型未出现

我尝试使用
右连接

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的血型


非常感谢

尝试使用
左外部联接
它返回左表中的所有行(用户),右表中的匹配行(血型)。当没有匹配项时,右侧的结果为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

您必须通过使用
交叉连接
然后
左连接
来获得
国家
和血型的所有组合。大概是这样的:

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 |