Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6931885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:38:07+00:00 2026-05-27T11:38:07+00:00

i have used the following query to get time range based count. subtract currentime

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:38:08+00:00Added an answer on May 27, 2026 at 11:38 am

    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.

    CREATE VIEW ticket_wait AS
      SELECT HOUR(TIMEDIFF(NOW(), created_date_time)) AS hdiff
      FROM ticket;
    

    After this, you can use the following query to get your results.

    SELECT COUNT(hdiff) count,
      CASE
        WHEN hdiff BETWEEN 0 AND 8 THEN '8Hrs'
        WHEN hdiff BETWEEN 9 AND 16 THEN '16Hrs'
        WHEN hdiff BETWEEN 17 AND 24 THEN '24Hrs'
      ELSE '>24Hrs'
    END `range`
    FROM ticket_wait
    GROUP BY `range`
    

    By using the above query on the table

    +----+---------------------+-------+
    | id | created_date_time   | issue |
    +----+---------------------+-------+
    |  1 | 2011-12-10 07:06:44 | foo   |
    |  2 | 2011-12-08 04:29:28 | foo   |
    |  3 | 2011-12-08 23:29:28 | foo   |
    |  4 | 2011-12-08 14:29:28 | foo   |
    |  5 | 2011-12-09 12:29:28 | foo   |
    |  6 | 2011-12-09 17:29:28 | foo   |
    |  7 | 2011-12-09 23:29:28 | foo   |
    |  8 | 2011-12-10 06:29:28 | foo   |
    |  9 | 2011-12-10 04:29:28 | foo   |
    | 10 | 2011-12-10 01:29:28 | foo   |
    +----+---------------------+-------+
    

    Your output will be

    +-------+--------+
    | count | range  |
    +-------+--------+
    |     1 | 16Hrs  |
    |     1 | 24Hrs  |
    |     5 | 8Hrs   |
    |     3 | G24Hrs |
    +-------+--------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have used the following statements to get the current time. print $query executed
I used the following query to find duplicates: SELECT userID, COUNT(userID) AS NumOccurrences FROM
I have the following query: SELECT location, step, COUNT(*), AVG(foo), YEAR(start), MONTH(start), DAY(start) FROM
I'm facing a hard time to get the following query: Let's say I have
sending mail along with embedded image using asp.net I have already used following but
Hi I have the following query running a function to get the Standard Deviation
I have the following Query and i need the query to fetch data from
i have used following code to repeat a process creation/close iteratively dim vProcessInfo as
I have used following code to create a simple PDF file. It executes fine
I have used the following as comments in my .emacs // {{{ // }}}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.