views:

85

answers:

2

i need Sql query Convert Table1 to Table2

select Count(Page) as VisitingCount,CONVERT(VARCHAR(5),Date, 108) as [Time] from scr_SecuristLog     
where Date between '2009-04-30' and '2009-05-02'    
and [user] in(select USERNAME               
    from scr_CustomerAuthorities where customerID=Convert(varchar,4) and ID=Convert(varchar,43) )    
group by CONVERT(VARCHAR(5),Date, 108) order by CONVERT(VARCHAR(5),Date, 108) asc 



Table1

VisitingCount Date
1-------------------15:09
3-------------------15:10
7-------------------15:15
1-------------------15:39
2-------------------15:40
3-------------------15:47



How can i change this table below table



Table2

VisitingCount Date
11-------------------15:00-15:30
6-------------------15:30-16:00
+1  A: 

Use a case statement to create a category, and then count by that category.

For (oversimplified) example.

select case when Date < '15:30' then '15:00 - 15:30'
            when Date < '16:00' then '15:30 - 16:00'
            else 'After 16:00' end as category
into #temp1
from Table1

select count(*) as VistingCount, category as Date
from #temp1
group by category
John M Gant
You can do this without the use of the temp table, just by grouping on the same CASE statement and doing the count
Tom H.
Very true. I thought this way would be clearer, but I should have mentioned that you could do it all in one statement (and avoid some unnecessary file I/O).
John M Gant
Thanks alot! Look please my another ques.http://stackoverflow.com/questions/829089/how-to-call-user-defined-function-in-order-to-use-with-select-group-by-order-by
Phsika
A: 

See my post in this other thread - http://stackoverflow.com/questions/824225/if-statement-sql/825013#825013

You can use a table that defines the bounds that you want to bucket each count by. E.g. you would have a bound definition like ('15:01', '15:30', '15:00 - 15:30'). Then you just join your data table against this boundaries table and include the time bucket in your GROUP BY (as shown in the other thread).