简体   繁体   English

每个类别的SQL计数

[英]SQL Count for Each Category

I need to count rows based on 2 fields for grouping. 我需要根据2个字段对行进行计数以进行分组。

Animals (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

Groups (g) 团体(g)

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

Strain (s) 应变

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

Group Animal Count (gac) 集体动物数(gac)

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

Mating History (mh) 交配历史(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

So when I group them by the strain_id and the death_cause this is what they should look like visually: 因此,当我通过strain_iddeath_cause将它们strain_id ,因为它们看起来应该是直观的:

Strain 1 ---- Culled 应变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

Strain 1 ---- Surplus 应变1 ----盈余

3      512        164           2015-12-02     Surplus       B

Strain 2 ---- Culled 应变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

Strain 2 ---- Surplus 应变2 ----盈余

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

What I want to get from the SQL query is the following result: 我想从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

Basically I want to count the number of animals using 2 categories which in this case is the strain_name and the death_cause 基本上我想用2个类别计算动物的数量,在这种情况下是strain_namedeath_cause

Note that for an animal to be counted as a breeder ( c_breeder ), I have to look at the Mating History table and check if the animal_id has ever had any of these codes MA or FA . 请注意,对于要被视为繁殖者( c_breeder )的动物,我必须查看“交配历史”表并检查animal_id是否曾经有过任何这些代码MAFA

I am using INNER JOIN on the groups , group_animal_count , and strains . 我使用INNER JOINgroupsgroup_animal_countstrains I use LEFT JOIN for mating_history since animals with a status of P won't have records in that table since they're just pups and won't be involved with mating. 我使用LEFT JOIN for mating_history因为状态为P动物将不会在该表中记录,因为它们只是幼崽并且不会参与交配。

Try: 尝试:

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

You can do the aggregation before joining the tables: 您可以在加入表之前进行聚合:

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 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM