【问题标题】:How can I COUNT the number of rows in a database for different time periods?如何计算数据库中不同时间段的行数?
【发布时间】:2011-08-06 08:20:06
【问题描述】:

我在 Sybase DB 中有一个表,其中有一列 createdDateTime。 我想要做的是计算在特定但累积的时间段之间创建了多少行,即:
7:00 - 7:15
7:00 - 7:30
7:00 - 7:45
7:00 - 8:00 ... 依此类推,直到我有最后一个时间组,7:00 - 18:00。

有没有一种很好的方法可以在 SQL 中进行一个查询,该查询将为我返回所有行以及所有行数:
时间             创建的行
7:00 - 7:15     0
7:00 - 7:30     5
7:00 - 7:45     8
7:00 - 8:00     15
...                   ...

目前我有一个解决方案,但它需要我运行参数化查询 44 次才能获取所有数据。

谢谢,

【问题讨论】:

    标签: sql time count sybase


    【解决方案1】:

    试试这个

     select count(*) from table groupedby createdDateTime where createdDateTime in (
    SELECT *
    FROM table 
    WHERE createdDateTime between createdDateTime ('2011/01/01:07:00', 'yyyy/mm/dd:hh:mm')
    AND createdDateTime ('2011/01/01:07:15', 'yyyy/mm/dd:hh:mm')
    )
    

    【讨论】:

    • 这不会汇总时间段,并且只有在时间段准确的情况下分组才会起作用。
    【解决方案2】:

    Sybase 是否有CASE 语句?如果是这样试试这个:

    SELECT SUM(CASE WHEN CreatedTime BETWEEN ('7:00:00' AND '7:14:59') THEN 1 ELSE 0) as '7-7:15',
           SUM(CASE WHEN CreatedTime BETWEEN ('7:15:00' AND '7:29:59') THEN 1 ELSE 0) as '7:15-7:30',
    FROM MyTable
    Where <conditions>
    

    我在 SQL Server 中经常使用它。

    【讨论】:

    • 这会更好,但这意味着必须对每个间隔进行硬编码:(
    • @Dhiren - 我不确定您在 Sybase 中有哪些时间函数,但您可以在一个组中使用一些日期/时间函数,例如 GROUP BY DatePart(hh, createdtime) 以按小时分组。不过,我不知道这需要 15 分钟的分解时间。
    【解决方案3】:

    您可以确定创建行的一刻钟并按该值分组。请注意,这是 Oracle SQL,但 Sybase 可能有一个等价物。

    select to_char(datetime_created, 'HH24') hour
    , floor(to_char(datetime_created, 'MI')/15)+1 quarter
    , count(1)
    from my_table
    group by to_char(datetime_created, 'HH24')
    , floor(to_char(datetime_created, 'MI')/15)+1;
    

    【讨论】:

      【解决方案4】:

      您的经期不规律(有些为 15 分钟,有些为 1 小时,有些为几个小时)。在这种情况下,您能做的最好的事情就是使用 case 语句运行查询:

      with thetable as
      (
      SELECT  'TM' code, convert(datetime, '2011-04-15 07:01:00 AM') date, 1 id union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:05:00 AM'), 2 union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:08:00 AM'), 3 union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:20:00 AM'), 4 union all
      SELECT  'TM', convert(datetime, '2011-04-15 08:25:00 AM'), 5 
      )
      
      SELECT '07:00 - 07:15' interval, sum(case when CONVERT(varchar, date, 108) between '07:00:00' AND '07:14:59' then 1 else 0 end) counting
      FROM thetable
      
      union 
      
      select '07:15 - 08:00', sum(case when CONVERT(varchar, date, 108) between '07:15:00' AND '07:59:59' then 1 else 0 end)
      from thetable
      
      union 
      
      select '08:00 - 09:00', sum(case when CONVERT(varchar, date, 108) between '07:59:59' AND '08:59:59' then 1 else 0 end)
      from thetable
      

      现在,如果你确实有固定的间隔,你会做这样的事情:

      select counting, 
      dateadd(ms,500-((datepart(ms,interval)+500)%1000),interval) intini
      from
      (
      SELECT COUNT(1) counting, CONVERT(datetime, round(floor(CONVERT(float, date) * 24 * 4) / (24 * 4), 11)) interval
      FROM 
      (
      SELECT  'TM' code, convert(datetime, '2011-04-15 07:01:00 AM') date, 1 id union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:05:00 AM'), 2 union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:08:00 AM'), 3 union all
      SELECT  'TM', convert(datetime, '2011-04-15 07:20:00 AM'), 4 union all
      SELECT  'TM', convert(datetime, '2011-04-15 08:25:00 AM'), 5 
      ) thetable
      group by FLOOR(CONVERT(float, date) * 24 * 4) 
      ) thetable2
      

      请注意,24 * 4 是 15 分钟的间隔。如果你的时间间隔是 1 小时,你应该用 24 代替。如果你的时间间隔是 10 分钟,它应该是 24 * 6。我想你明白了。

      【讨论】:

      • 顺便说一下,我不知道这会转化为 sybase,我使用的是 SQL Server 2008。
      【解决方案5】:

      我最近写了一篇关于这个确切主题的博客,但不确定它是否适用于 Sybase,这是解决方案

      declare @interval int
      set @interval = 5
      select datepart(hh, DateTimeColumn)
      , datepart(mi, DateTimeColumn)/@interval*@interval
      , count(*)
      from thetable
      group by datepart(hh, DateTimeColumn)
      , datepart(mi, DateTimeColumn)/@interval*@interval
      

      还有更多细节 http://ebersys.blogspot.com/2010/12/sql-group-datetime-by-arbitrary-time.html

      【讨论】:

      • 这不是多余的吗: datepart(mi, DateTimeColumn)/@interval*@interval 如果你除以间隔,然后除以它,你实际上就剩下你开始的东西了吗?
      • @DHiren:如果是整数除法,则不会,并且小数会被截断;本质上它变成了一个周期性的 Floor() 函数。您没有提到哪个 Sybase 数据库,但在 ASE SELECT 7/5*5 中返回 5,因为 7/5 部分被截断为 1,因为它被强制转换为整数。
      • @Terry:这几乎是我所需要的,只是它不会产生滚动总和,但我可以稍后在代码中做到这一点,谢谢!
      猜你喜欢
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      • 2010-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      • 1970-01-01
      • 2020-07-01
      相关资源
      最近更新 更多