sql - 在数据库中查询第 "trim"行

标签 sql database postgresql

在 PostgreSQL 中有没有什么方法可以根据规则忽略末尾的行,并且在不使用 2 个子查询的情况下具有良好的性能?

例如,我想通过忽略列表开头和列表末尾小于 1 的值来从列表中获取值:

ROWS:   0, 0.5, 0, 0.2, 0, 0.8, 1, 1.2, 1.3, 1, 0.9, 0, 0.2, 3, 2, 1, 0.9, 0.5, 0, 0.1, 0
SELECT: .......................[1, 1.2, 1.3, 1, 0.9, 0, 0.2, 3, 2, 1]....................

谢谢!!

最佳答案

你可以用不同的方式表达。您需要第一个等于或大于 1 的值与最后一个此类值之间的所有行。

假设您有一个对行进行排序的唯一 ID,您可以:

select t.*
from (select t.*,
             min(id) filter (where value >= 1) over () as minid1,
             max(id) filter (where value >= 1) over () as maxid1
      from t
     ) t
where id >= minid1 and id <= maxid1;

关于sql - 在数据库中查询第 "trim"行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52105664/

相关文章:

ruby-on-rails - 对于 database.yml 中的池大小,我应该考虑什么?

python - 为 Django 中两个不同类的选项创建外键

sql - 如何在 linux shell 上的 sql 查询中使用 "%"字符?

创建表时出现 MySQL 错误 1005

database - ORA-00060 : deadlock detected while waiting for resource

sql - postgres加入同一张表

sql - 选择重复两次的记录!

database - 将数据库复制到其他系统的最佳方法

mysql - 先按完全匹配顺序对 Op.or order 进行 Sequelize

database-design - 数据库设计 : best practices to hierarchical structure