views:

90

answers:

2

Hi all, i have one table in my database say mytable, which contents request coming from other source. There is one column in this table as Time, which stores date and time(e.g. 2010/07/10 01:21:43) when request was received. Now i want to fetch the data from this table on hourly basis for each day. Means i want count of requests database receive in each hours of a day. e.g.for 1 o'clock to 2 o'clock say count is 50 ..like this.. I will run this query at the end of day. So i will get requests received in a day group by each hour.

Can anybody help me in this.

I want query which will take less time to fetch the data as my database size is huge.

Any othre way than OMG Ponies answer.

+7  A: 

Use the TO_CHAR function to format the DATETIME column, so you can GROUP BY it for aggregate functions:

  SELECT TO_CHAR(t.time, 'YYYY-MM-DD HH24') AS hourly,
         COUNT(*) AS numPerHour
    FROM YOUR_TABLE t
GROUP BY TO_CHAR(t.time, 'YYYY-MM-DD HH24')
OMG Ponies
i like it, so +1. But can we do it without using join.Because my data is huge.
MAS1
@MAS1: Sorry, but I've re-read your question and I don't see anything implying the need for a JOIN.
OMG Ponies
@OMG Ponies, i think u use self join in your answer.(t.time)
MAS1
@MAS1: No, there's no self join--you'd see the "JOIN"/etc keyword if that were the case.
OMG Ponies
@OMG Ponies,ok..Thanks. Will try it now.
MAS1
@MAS1 that's not a join, it's using the table alias. If you're only looking for data for the current day you can add `WHERE t.time BETWEEN TRUNC(sysdate) AND TRUNC(sysdate + 1)` between the `FROM` and `GROUP BY` clauses; or you can parameterise that to use a date you pass in.
Alex Poole
@Alex I think you meant (sysdate + 1) there (not + 10)
Tony Andrews
@Tony thanks, mis-corrected a typo and made it worse. Fixed it.
Alex Poole
@Alex and @Tony, Will try this also.
MAS1
@MAS1: At the risk of stating the obvious, if you don't already have an index on the Time field on your table, you should add one if you can. If the query still takes too long, consider a separate partition on the table for the current day's data.
Mark Bannister
@Mark Bannister: The use of TO_CHAR would render the index on the `time` column useless, unfortunately.
OMG Ponies
@ Mark Bannister:Thanks for your suggestion of separation of current day's data.
MAS1
@OMG: the `WHERE` clause would still use the index to restrict to a single day, and avoid the need to fetch the data blocks for the records that match as well (although I think I read somewhere that `count(*)` could undo some of that and `count(1)` might be more performant - must test that one day).
Alex Poole
+2  A: 
sh_kamalh
@sh_kamalh..+1,Thanks for your suggestion..but i can't create another table.
MAS1