AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / dba / 问题 / 336080
Accepted
Ashley
Ashley
Asked: 2024-02-21 11:52:14 +0800 CST2024-02-21 11:52:14 +0800 CST 2024-02-21 11:52:14 +0800 CST

选择随机记录,直到超过累计总数

  • 772

我有一个带有id,skill_level和duration列的视频表,我正在尝试编写一个查询,该查询将返回总持续时间超过 的不同视频的播放列表X。

select _id, duration, running_total
from (select *, sum(duration) over (order by RANDOM()) as running_total
      from videos
      where skill_level in ('beginner', 'superbeginner') AND watched IS NULL
     )
where running_total <= 7200;

这有效并返回以下内容:

5e4d11fbaac87f3820954c45|305|305
64e51ad279223a9ad03525c2|930|1235
5e4d1204aac87f3820954c64|627|1862
5e4d1212aac87f3820954c95|367|2229
641e05c2d7c1512d278dc51a|361|2590
5e4d1200aac87f3820954c57|444|3034
649aa7089776ba12d8de28d5|329|3363
5e4d123eaac87f3820954d2c|573|3936
5e4d1216aac87f3820954ca1|399|4335
5e4d120daac87f3820954c84|315|4650
5e4d11dfaac87f3820954be1|510|5160
64f7036d3d578af923ba2afc|326|5486
5e4d11fcaac87f3820954c47|437|5923
5fa51afc27a912152cc51c33|1062|6985

但理想情况下,我想返回足够多的记录,X但只能超过一条。6375274c1cff4a7e31b04a0d下面让我们结束7200。

5e4d11fbaac87f3820954c45|305|305
64e51ad279223a9ad03525c2|930|1235
5e4d1204aac87f3820954c64|627|1862
5e4d1212aac87f3820954c95|367|2229
641e05c2d7c1512d278dc51a|361|2590
5e4d1200aac87f3820954c57|444|3034
649aa7089776ba12d8de28d5|329|3363
5e4d123eaac87f3820954d2c|573|3936
5e4d1216aac87f3820954ca1|399|4335
5e4d120daac87f3820954c84|315|4650
5e4d11dfaac87f3820954be1|510|5160
64f7036d3d578af923ba2afc|326|5486
5e4d11fcaac87f3820954c47|437|5923
5fa51afc27a912152cc51c33|1062|6985
6375274c1cff4a7e31b04a0d|1138|8123

是否可以在 sqlite 中编写查询来实现此目的?

query
  • 1 1 个回答
  • 13 Views

1 个回答

  • Voted
  1. Best Answer
    White Owl
    2024-02-21T22:42:12+08:002024-02-21T22:42:12+08:00

    换句话说,任务是获取总和小于阈值的记录列表,并添加一条超过阈值的记录?

    with这可以通过使用andunion子句来实现:

    with
    below_threshold as (
      select _id, duration, running_total
      from (select *, sum(duration) over (order by RANDOM()) as running_total
            from videos
            where skill_level in ('beginner', 'superbeginner') AND watched IS NULL
           )
      where running_total <= 7200
    ),
    one_more as (
      select _id, duration
      from videos
      where skill_level in ('beginner', 'superbeginner') AND watched IS NULL
            and _id not in (select _id from below_threshold)
      limit 1
      order by random()
    )
    select _id, duration from below_threshold
    union
    select _id, duration from one_more
    

    或者您可以进行预选:

    with
    to_consider as (
      select _id, duration
      from videos
      where skill_level in ('beginner', 'superbeginner') AND watched IS NULL
    ),
    below_threshold as (
      select _id, duration, running_total
      from (select *, sum(duration) over (order by RANDOM()) as running_total
            from to_consider
           )
      where running_total <= 7200
    ),
    one_more as (
      select _id, duration, (select sum(duration) from below_threshold)+duration as running_total
      from to_consider
      where _id not in (select _id from below_threshold)
      limit 1
      order by random()
    )
    select _id, duration, running_total from below_threshold
    union
    select _id, duration, running_total from one_more
    
    • 1

相关问题

  • 在外键值上查询 PostgreSQL 9.0 表?

  • 如何获取用户好友的姓名?

  • 两个相关表之间的查询

  • 日期对齐和对匹配提取最好用 TSQL 或 C# 完成?

  • LIKE 选择文字中任意位置独立存在的单词

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve