加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Sqlite3:需要笛卡儿约会

发布时间:2020-12-12 18:57:41 所属栏目:百科 来源:网络整理
导读:我有一个表,它是在sqlite3数据库中播放的游戏列表.字段“datetime”是游戏结束时的日期时间.字段“持续时间”是游戏持续的秒数.我想知道过去24小时里有多少百分比至少有5场比赛同时进行.我想知道在给定时间运行的游戏有多少: select count(*)from gameswher
我有一个表,它是在sqlite3数据库中播放的游戏列表.字段“datetime”是游戏结束时的日期时间.字段“持续时间”是游戏持续的秒数.我想知道过去24小时里有多少百分比至少有5场比赛同时进行.我想知道在给定时间运行的游戏有多少:

select count(*)
from games
where strftime('%s',datetime)+0 >= 1257173442 and
      strftime('%s',datetime)-duration <= 1257173442

如果我的桌子只是每秒钟(或每30秒钟或某些东西)的列表,我可以做一个有意的卡地亚产品,如下所示:

select count(*)
from (
  select count(*) as concurrent,d.second
  from games g,date d
  where strftime('%s',datetime)+0 >= d.second and
        strftime('%s',datetime)-duration <= d.second and
        d.second >= strftime('%s','now') - 24*60*60 and
        d.second <= strftime('%s','now')
  group by d.second) x
where concurrent >=5

有没有办法动态创建这个日期表?或者我可以得到类似的效果,而不必实际创建一个新表,这只是本周所有秒的列表?

谢谢

解决方法

好问题!

这是一个我认为可以在不使用单独表格的情况下为您提供所需内容的查询.请注意,这是未经测试的(因此可能包含错误),我假设datetime是一个带有秒数的int列,以避免大量的strftime.

select sum(concurrent_period) from (
  select min(end_table.datetime - begin_table.begin_time) as concurrent_period
  from (
    select g1.datetime,g1.num_end,count(*) as concurrent
    from (
      select datetime,count(*) as num_end
             from games group by datetime
    ) g1,games g2
    where g2.datetime >= g1.datetime and
          g2.datetime-g2.duration < g1.datetime and
          g1.datetime >= strftime('%s','now') - 24*60*60 and
          g1.datetime <= strftime('%s','now')+0
  ) end_table,(
    select g3.begin_time,g1.num_begin,count(*) as concurrent
    from (
      select datetime-duration as begin_time,count(*) as num_begin
             from games group by datetime-duration
    ) g3,games g4
    where g4.datetime >= g3.begin_time and
          g4.datetime-g4.duration < g3.begin_time and
          g3.begin_time >= strftime('%s','now') - 24*60*60 and
          g3.begin_time >= strftime('%s','now')+0
  ) begin_table
  where end_table.datetime > begin_table.begin_time
        and begin_table.concurrent < 5
        and begin_table.concurrent+begin_table.num_begin >= 5
        and end_table.concurrent >= 5
        and end_table.concurrent-end_table.num_end < 5
  group by begin_table.begin_time
) aah

基本思路是制作两个表:一个在每个游戏开始时使用#个并发游戏,另一个在结束时使用#并发游戏.然后将这些表连接在一起,只在“关键点”处获取行,其中并发游戏的数量超过5.对于每个关键的开始时间,请采取最快发生的关键结束时间,并希望提供至少5个游戏正在运行的所有时段同时.

希望不是太复杂有用!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读