在Postgres SELECT语句中选择WHERE子句中特定行的计数

时间:2017-03-21 01:45:58

标签: sql select where-clause psql

假设我有一个名为Build的表和一个名为Test的表。现在,对于每个构建,都有3个测试。表测试将build_id作为外键。此外,当测试失败时,我的测试框架会再次重试测试。鉴于此,测试表中至少有3个条目+更多基于测试失败的次数和重试的次数(最大值= 3)。此外,重试后,如果测试通过,则测试标记为已通过。

DATABASE
+----------+----------------------+
| title    |    date_issued       |
+----------+----------------------+
| TITLE_1  |    2017-03-04        |
+----------+----------------------+
| TITLE_2  |    2017-03-17        |
+----------+----------------------+
| TITLE_3  |    2017-03-20        |
+----------+----------------------+
| TITLE_4  |    2017-04-01        |
+----------+----------------------+


AND I WANT TO BECOME WITH TO DISPLAY IN MY WEBSITE



   **ARCHIVE MENU**
   March 2017
   April 2017
   and so on...

现在,基于我想打印

Sample:
Build Table:
Id     No of Tests     Failure Count
1            3                     0
2            3                     0
3            3                     1


Tests Table: 
build_id        test_id       test_name     test_result
1                      1                test1                p
1                      2                test2                p
1                      3                test3                f
1                      3                test3                f
1                      3                test3                f
1                      3                test3                p
2                      1                test1                p
2                      2                test2                p
2                      3                test3                p
3                      1                test1                p
3                      2                test2                f
3                      3                test3                p
2                      2                test2                f
2                      2                test2                f
2                      2                test2                f

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要测试的次数" f":

select test_name, count(*) as retries
from tests
where test_result = 'f'
group by test_name;