每个类别的SQL计数

时间:2016-02-29 21:36:13

标签: sql oracle count group-by

我需要根据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是否曾有过任何这些代码{{1 }或MA

我在FAINNER JOINgroups上使用group_animal_count。我对strains使用LEFT JOIN,因为状态为mating_history的动物将不会在该表中记录,因为它们只是幼崽并且不会参与交配。

2 个答案:

答案 0 :(得分:0)

尝试:

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

答案 1 :(得分:0)

您可以在加入表格之前进行聚合:

SELECT group_name, strain_name, death_cause, total
FROM   (
         SELECT   group_id,
                  strain_id,
                  death_cause,
                  COUNT(*) AS total
         FROM     animals
         GROUP BY group_id, strain_id, death_cause
       ) a
       INNER JOIN groups g ON ( g.group_id = a.group_id )
       INNER JOIN strain s ON ( s.strain_id = a.strain_id );