我需要根据2个字段对行进行分组。

动物(a)

id     group_id   strain_id     death_date     death_cause   status
-----------------------------------------------------------------------
1      512        164           2015-12-01     Culled        P
2      512        164           2015-12-02     Culled        A
3      512        164           2015-12-02     Surplus       B
4      512        230           2015-12-06     Culled        A
5      512        164           2015-12-28     Culled        A
6      512        230           2016-01-20     Culled        B
7      512        230           2016-01-20     Surplus       P
8      512        164           NULL           NULL          P
9      512        230           NULL           NULL          B
10     512        230           NULL           NULL          A
11     512        164           2016-01-25     Culled        B
12     512        164           2016-02-29     Culled        A
13     512        230           2016-02-03     Surplus       P
14     512        230           2016-02-03     Culled        A


组(g)

id     group_name
--------------
512    Mice


应变

id     strain_name
----------------
164    Strain 1
230    Strain 2


团体动物数量(gac)

id     total_animals      alive_count       dead_count
----------------------------------------------------------------------
512    14                 3                 11


交配历史(mh)

id     animal_id     history_type      history_date
--------------------------------------------------------
1001   2             MA                2015-11-20
1002   2             MR                2015-12-01
1003   3             MA                2015-12-01
1004   6             FA                2015-12-21
1005   9             FA                2016-02-07
1006   10            MA                2016-01-27
1007   11            FA                2015-12-12


因此,当我按strain_iddeath_cause对它们进行分组时,这应该是它们的外观:

应变1 ----剔除

1      512        164           2015-12-01     Culled        P
2      512        164           2015-12-02     Culled        A
5      512        164           2015-12-28     Culled        A
11     512        164           2016-01-25     Culled        B
12     512        164           2016-02-29     Culled        A


应变1 ----剩余

3      512        164           2015-12-02     Surplus       B


应变2 ----剔除

4      512        230           2015-12-06     Culled        A
6      512        230           2016-01-20     Culled        B
14     512        230           2016-02-03     Culled        A


应变2 ----剩余

7      512        230           2016-01-20     Surplus       P
13     512        230           2016-02-03     Surplus       P


我想从SQL查询中得到以下结果:

g_name  s_name    d_cause  a_total  c_alive  c_dead  c_pup  c_breeder  c_total
------------------------------------------------------------------------------
Mice    Strain 1  Culled   12       3        9       1      2          5
Mice    Strain 1  Surplus  12       3        9       0      1          1
Mice    Strain 2  Culled   12       3        9       0      1          3
Mice    Strain 2  Surplus  12       3        9       2      0          2


基本上,我想使用2个类别(在这种情况下为strain_namedeath_cause

请注意,对于要算作繁殖者(c_breeder)的动物,我必须查看“交配历史”表,并检查animal_id是否曾经有以下任何一种代码MAFA

我在INNER JOINgroupsgroup_animal_count上使用strains。我将LEFT JOIN用于mating_history,因为状态为P的动物在该表中不会有记录,因为它们只是幼崽,不会参与交配。

最佳答案

尝试:

SELECT group_name, strain_name,death_cause, count(*) as Total
FROM ANIMALS a
JOIN GROUPS g ON a.group_id = g.id
JOIN STRAIN s ON a.strain_id = s.id
GROUP BY group_name, strain_name,death_cause

09-26 09:37