以下是数据表--

 del_no  |   Pkt   | direction  |  Env  | start_datetimestamp |
---------+---------+------------+-------+---------------------+
 H_00002 |  02     |  SOUTH     | PROD  | 2022-10-29 16:20:57 |
 E20     |  20     |  NORTH     | PROD  | 2022-10-30 16:41:37 |
 H_00002 |  02     |  NORTH     | TEST  | 2022-10-30 17:21:17 |
 E20     |  20     |  SOUTH     | DEV   | 2022-10-30 17:30:24 |
 H_00004 |  02     |  NORTH     | PROD  | 2022-10-30 16:52:48 |
 H_00004 |  02     |  SOUTH     | PROD  | 2022-10-30 19:03:36 |
 H_00007 |  02     |  NORTH     | PROD  | 2022-10-30 20:52:48 |
 H_00007 |  02     |  SOUTH     | PROD  | 2022-10-30 21:03:36 |
 H_00015 |  02     |  SOUTH     | TEST  | 2022-11-13 19:11:10 |
 L 0013  |  13     |  NORTH     | PROD  | 2022-11-14 20:06:46 |
 H_00015 |  02     |  NORTH     | TEST  | 2022-11-15 20:17:40 |
 L0021   |  21     |  SOUTH     | TEST  | 2022-11-15 20:56:18 |
 H_00015 |  02     |  NORTH     | PROD  | 2022-11-15 20:17:40 |
 L0027   |  21     |  SOUTH     | DEV   | 2022-11-30 20:56:18 |
 H_00019 |  02     |  NORTH     | PROD  | 2022-11-30 20:17:40 |
 L0023   |  21     |  SOUTH     | TEST  | 2022-11-30 20:56:18 |
 H_00019 |  02     |  SOUTH     | TEST  | 2022-11-30 20:17:40 |
 L0025   |  21     |  SOUTH     | TEST  | 2022-11-30 20:56:18 |
 H_00019 |  02     |  SOUTH     | DEV   | 2022-11-30 20:17:40 |
 H_00018 |  02     |  SOUTH     | PROD  | 2023-10-31 20:17:40 |
 H_00018 |  02     |  NORTH     | PROD  | 2023-11-02 03:17:40 |
 H_00033 |  02     |  SOUTH     | PROD  | 2023-10-31 20:17:40 |
 H_00033 |  02     |  NORTH     | DEV   | 2023-11-02 03:17:40 |

我想数一下以下条件下的子弹数-

1- Filter by Pkt where Pkt = 02 only.
2- Group by 'del_no' (consider NORTH and SOUTH one complete round), then counts all the rounds by monthly and yearly.
3- Segregate the counts based on Env. (e.g. 1st and 3rd row is one complete round and both has PROD and TEST env so that one count should be in PROD/TEST Env, same goes for other).
4- If same del_no(H_00018) starts on another month and end on next month so the count should be added in start month.

输出-

Month   |       Env     | Counts |
--------+---------------+--------+
2022-10 | PROD/TEST     |  1     |
2022-10 | PROD          |  2     |
2022-11 | PROD/TEST     |  1     |
2022-11 | PROD/TEST/DEV |  1     |
2023-10 | PROD          |  1     |
2023-10 | PROD/DEV      |  1     |

以下查询在第4个条件下不起作用-

select "Month",sum(rounds)"Counts","Env" from (
    select del_no,
           to_char(start_datetimestamp,'YYYY-MM') "Month",
           least( count(*)filter(where direction='SOUTH')
                 ,count(*)filter(where direction='NORTH')) rounds,
           string_agg(distinct env,'/' order by env) "Env"
    from tablename where pkt='2' group by del_no,2)
group by "Month","Env";

https://dbfiddle.uk/FGIzl6hy

推荐答案

您可以使用多个CTE:

with cte as (
  select del_no
  from tableName
  where Pkt = '2'
  group by del_no
  having count(distinct direction) = 2
),
cte2 as (
  select t.del_no,
         to_char(min(start_datetimestamp), 'YYYY-MM') as _Month, 
         string_agg(distinct env, '/') as Env
  from cte c
  inner join tableName t on c.del_no = t.del_no
  group by t.del_no
)
select _Month, env, count(env) as Counts
from cte2
group by _Month, env
  • 第一个CTE被用来过滤掉任何del_no个不完整的回合.

  • 第二次CTE用于获得环境组合,每del_no个月开始

  • 然后,我们按月份和环境进行汇总,以获得计数.

Demo here

Postgresql相关问答推荐

如何将Postgs SUM结果存储在多个变量中?

如何接受jsonb路径中的任何密钥?

为什么在PostgreSQL中CPU_INDEX_TUPLE_COST与CPU_TUPLE_COST不同

在同一 Select 列表中两次使用jsonb_arrayElements()是否安全?

supabase 中的交易

postgres 如何计算多列哈希?

为什么这里的默认格式不同? (ISO 8601 的两种不同实现)

是否可以在 postgresql 中添加表元数据?

使用 JDBC 连接到 PostgreSql 的本地实例

PostgreSQL - 改变数字的精度?

OpenShift:如何从我的 PC 连接到 postgresql

PostgreSQL - jsonb_each

无法向 postgresql 数据库授予用户权限(对于 rails 应用程序)

Flask-SQLAlchemy db.session.query(Model) 与 Model.query

并发刷新materialized视图

在 Spring Boot 上使用 Hibernate 映射 PostGIS 几何点字段

IntegrityError:postgres 从转储恢复后,所有具有 ForeignKey 的模型/字段的id列中的空值

在不存在的行上有select for update块

Postgres:为 CAST 失败定义一个默认值?

如何在我的 Mac 上卸载 postgresql(运行 Snow Leopard)