i have used the following query to get time range based count. subtract currentime from created time but it produces an error
Error code 1064, SQL state 42000: You have an error in your SQL
syntax; check the manual that corresponds to your MySQL server version
for the right syntax to use near ‘TO 8 then ‘8Hrs’
WHEN round(time_to_sec(timediff(now() , `created_date_t’ at line 4
SELECT t.range, count(*) as num
FROM (
SELECT CASE
WHEN round(time_to_sec(timediff(now() , `created_date_time`))/3600) BETWEEN 0 TO 8 then '8Hrs'
WHEN round(time_to_sec(timediff(now() , `created_date_time`))/3600) BETWEEN 9 TO 16 then '16Hrs'
WHEN round(time_to_sec(timediff(now() , `created_date_time`))/3600) BETWEEN 17 TO 24 then '24Hrs'
WHEN round(time_to_sec(timediff(now() , `created_date_time`))/3600) > 24 then 'G24Hrs'
AS range
FROM `ticket`
) as t
GROUP BY range
And i have to avoid repetion of
round(time_to_sec(timediff(now() , created_date_time))/3600)
statement.
table structure
id created_date_time issue
-------------------------------------
1 2011-12-07 05:29:28 test
2 2011-12-08 07:56:15 test
3 2011-12-08 05:56:15 test
create query
CREATE TABLE `ticketingsystem`.`ticket` (
`id` bigint( 20 ) NOT NULL AUTO_INCREMENT ,
`created_date_time` datetime NOT NULL ,
`issue` text NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM DEFAULT CHARSET = latin1
I want below output
these fields are no of records has pending time since record created
8hrs 16hrs 24hrs >24hr
---------------------------------
3 2 6 4
There are a lot of mistakes in your query, so I’ve rewritten it all instead.
First of all, I suggest you create a
VIEW. This is a one time thing.After this, you can use the following query to get your results.
By using the above query on the table
Your output will be