子表中的SQL计数

子表中的SQL计数,sql,sql-server-2012,Sql,Sql Server 2012,我想计算子表中的一些列。我的表格结构如下: 人 +---+----------+--------+ | Pid | Name |Surname | +---+----------+--------+ | 1 | Per A | D | | 2 | Per B | E | | 3 | Per C | F +----+---------+--------+ +---+---------+-------------------+---------

我想计算子表中的一些列。我的表格结构如下:

+---+----------+--------+
| Pid | Name   |Surname |
+---+----------+--------+
| 1   | Per A  | D      |
| 2   | Per B  | E      |
| 3   | Per C  | F      
+----+---------+--------+
+---+---------+-------------------+------------+-----+
| Cid | CName          | School   | Sex        | Pid |
+---+---------+-------------------+------------+-----+
| 1   | John           | High     |    Man     | 1   |
| 2   | Alice          | Primary  |    Woman   | 2   |
| 3   | Mel            | High     |    Man     | 3   |
| 4   | Angelina       | High     |    Woman   | 2   |
+----+---------+------------------+------------+-----+
儿童

+---+----------+--------+
| Pid | Name   |Surname |
+---+----------+--------+
| 1   | Per A  | D      |
| 2   | Per B  | E      |
| 3   | Per C  | F      
+----+---------+--------+
+---+---------+-------------------+------------+-----+
| Cid | CName          | School   | Sex        | Pid |
+---+---------+-------------------+------------+-----+
| 1   | John           | High     |    Man     | 1   |
| 2   | Alice          | Primary  |    Woman   | 2   |
| 3   | Mel            | High     |    Man     | 3   |
| 4   | Angelina       | High     |    Woman   | 2   |
+----+---------+------------------+------------+-----+
所以我想输出

+---+----------+------+---------+--------+---+--------------+
| Pid| PerName | High | Primary | Woman  | Man | ChildCount | 
+---+----------+------+---------+--------+-----+------------+
| 1  | Per A   | 1    | 0       | 0      | 1   | 1          |
| 2  | Per B   | 1    | 1       | 2      | 0   | 2          |
| 3  | Per C   | 1    | 0       | 0      | 1   | 1          |
+----+---------+------+---------+--------+-----+------------+
如何获得此输出?

我尝试了这种方法,但我有更多这样的列要计算,它们属于子表。所以我得到的查询结果很慢

select Pid,Name,Surname,
  (select count(*) from Childs where Persons.Pid=Childs.Pid) ChildCount, 
  (select count(*) from Childs where Persons.Pid=Childs.Pid and School='Primary') Primary 
from Persons

您可以使用
join
和条件聚合来执行此操作:

select p.Pid, p.Name,
       sum(case when c.school = 'High' then 1 else 0 end) as high,
       sum(case when c.school = 'Primary' then 1 else 0 end) as primary,
       sum(case when c.sex = 'Man' then 1 else 0 end) as Man,
       sum(case when c.sex = 'Woman' then 1 else 0 end) as Woman,
       count(*) as ChildCount
from persons p left join
     childs c
     on p.pid = c.pid
group by p.Pid, p.Name;
试试这个:

select Pid,Name,Surname,
ifNull((select count(*) from Childs where Persons.Pid=Childs.Pid),0) ChildCount,
ifNull((select count(*) from Childs where Persons.Pid=Childs.Pid AND School='High' GROUP By Childs.Pid),0) High,
ifNull((select count(*) from Childs where Persons.Pid=Childs.Pid AND School='Primary' GROUP By Childs.Pid),0) 'primary',
ifNull((select count(*) from Childs where Persons.Pid=Childs.Pid AND Sex='Woman' GROUP By Childs.Pid),0) Woman,
ifNull((select count(*) from Childs where Persons.Pid=Childs.Pid AND Sex='Man' GROUP By Childs.Pid),0) Man

from Persons;

加入表格。执行分组依据。我只想使用Pid进行分组,但SQL需要其他列,如名称、姓氏等。如果在mysql中没有问题,但在mssql问题中较新的mysql版本不允许您执行无效的分组依据-除非在兼容模式下。我只想使用Pid进行分组,并想选择名称、姓氏,但SQL需要其他列,如名称,姓氏等,因为名字相同。我们怎么能做到呢