如何在不同时间段内计算数据库中的行数?

时间:2011-04-15 13:14:54

标签: sql time count sybase

我在Sybase DB中有一个表创建了一个具有创建日期的列。 我想要做的是计算在特定但累积的时间段之间创建了多少行,即:
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次以获取所有数据。

谢谢,

5 个答案:

答案 0 :(得分:3)

我最近在博客上写了这个确切的主题,不确定它是否适用于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

答案 1 :(得分:0)

试试这个

 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 :(得分:0)

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中使用了很多。

答案 3 :(得分:0)

您可以确定创建行的小时数,并按该值进行分组。请注意,这是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 :(得分:0)

你有不规则的时期(有些是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.我认为你已经得到了图片。

相关问题