2011-04-15 46 views
3

我已經在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       7:00 - 7:30       7:00 - 7:45       7:00 - 8:00       ...                                       ...

我目前有一個解決方案,但它需要我運行參數化查詢44次才能獲取所有數據。

感謝,

回答

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

+0

這是不是多餘的: datepart(mi,DateTimeColumn)/ @interval * @interval 如果按區間劃分,然後按時間劃分,那麼您實際上留下的是開頭的東西嗎? – Dhiren 2011-04-15 14:48:53

+0

@DHiren :如果它是整數除法,並且小數被截斷;本質上它變成了一個週期性的Floor()函數,你不會提到哪個Sybase數據庫,但是在ASE SELECT 7/5 * 5中返回5,因爲7/5部分被截斷爲1,因爲它被轉換爲整數。 – Terry 2011-04-15 16:11:37

+0

@Terry:這幾乎是我所需要的,除非它不產生滾動的總和,但我可以在代碼後面做到這一點,謝謝! – Dhiren 2011-04-15 16:34:55

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') 
) 
+0

這不會聚集的時間段,以及只有在時間段精確的情況下,group by纔會起作用。 – Dhiren 2011-04-15 13:54:38

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

+0

這會更好,但它意味着必須硬編碼每個區間:( – Dhiren 2011-04-15 13:55:18

+0

@Dhiren - 我不知道你在Sybase有什麼時間功能,但你可以在一個組中使用一些日期/時間函數,像'GROUP BY DatePart(hh,createdtime)'按小時分組,我不知道這個15分鐘的故障。 – JNK 2011-04-15 13:58:43

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; 
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。我認爲你得到了這張照片。

+0

順便說一句,我不知道那會轉化爲sybase,我使用SQL Server – 2011-04-15 14:13:37

相關問題