使用join和'where'子句计算数据计数

时间:2017-05-12 05:12:14

标签: sql sql-server

我的表格中包含[Dist_Id][Name][FatherContactNumber]列。

现在我需要一个地区所有儿童的人数和FatherContactNumber != ''的儿童人数。

我创建了查询以查找FatherContactNumber != ''所有子项,但我还需要孩子总数。

select 
    d.Dist_Name, count(*) [Total_Childernd] 
from 
    Children c
inner join 
    District d on c.Dist_Id = d.Dist_Id
where 
    c.FatherContactNumber != ''
group by 
    d.Dist_Name

输出:

    Dist_Name        Total_Childernd_With_Numbers
-------------------------------------------
    Dist_Name1       313
    Dist_Name2       215
    Dist_Name3       1624
    Dist_Name4       38
    Dist_Name5       2874
    Dist_Name6       118
    Dist_Name7       72
    Dist_Name8       3590

我想要的输出是

  Dist_Name     Total_Childernd_With_Parent_Numbers       Total_Childernd
----------------------------------------------------------------------------
    Dist_Name1    |   313                             |
    Dist_Name2    |   215                             |
    Dist_Name3    |   1624                            |
    Dist_Name4    |   38                              |
    Dist_Name5    |   2874                            |
    Dist_Name6    |   118                             |
    Dist_Name7    |   72                              |
    Dist_Name8    |   3590                            |

我不需要创建嵌套查询。

请建议最好的方法,因为我有数十亿的记录。

0 个答案:

没有答案