0

I have this query which works fine and giving the expected result.

SELECT count(*) AS total_unsolved,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null) as today_to_solve,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null) as days_left_1_3,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null) as days_left_4_7,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null) as mt_week,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null) as overdue_1_3_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null) as overdue_4_10_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null) as overdue_11_30_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null) as overdue_mt_month,
    (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null) as solved_with_delay  
 from my_table where response_time is null 

enter image description here

I would like to create a procedure that shows the result in a given time range. So I typed the following:

CREATE PROCEDURE status_in_timerange
     (
        IN   start_date                     TiMESTAMP, 
        IN   close_date                     TiMESTAMP,
        OUT  total_unsolved                 INT,  
        OUT  today_to_solve                 INT, 
        OUT  days_left_1_3                  INT, 
        OUT  days_left_4_7                  INT, 
        OUT  mt_week                        INT, 
        OUT  overdue_1_3_days               INT, 
        OUT  overdue_4_10_days              INT,      
        OUT  overdue_11_30_days             INT,    
        OUT  overdue_mt_month               INT,    
        OUT  solved_with_delay              INT    
     )
BEGIN 

    SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 

    INTO   total_unsolved , 
           today_to_solve, 
           days_left_1_3, 
           days_left_4_7, 
           mt_week, 
           overdue_1_3_days ,
           overdue_4_10_days,
           overdue_11_30_days,
           overdue_mt_month,
           solved_with_delay

  FROM my_table WHERE response_time is null and ktimestamp BETWEEN start_date and close_date
END ;

Unfortunately, this query is showing an error as below:

SQL Error (1064): 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 'END' at line 41

I have read related questions (including the following) which didn't help to solve the issue.

Mysql stored procedure error

Mysql stored procedure error 1064

P.S. select version(); returns 5.6.21

9
  • 1
    Please help me out to find the mistake from the stored procedure Commented Jul 15, 2017 at 7:00
  • Have a look into inmotionhosting.com/support/website/database-troubleshooting/… Commented Jul 15, 2017 at 7:12
  • Add a semicolon (;) at the end of the statement: ... WHERE response_time is null and ktimestamp BETWEEN start_date and close_date;. Commented Jul 15, 2017 at 7:23
  • @sForSujit I have gone through that article and video which explained common cases with Error 1064. But I couldn't find any of these mistakes in my stored procedure. If you could see any, please give me a hint Commented Jul 15, 2017 at 7:35
  • @wchiquito using delimiter at that point caused new error at the line: SQL Error (1064): 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 " at line 40 Commented Jul 15, 2017 at 7:38

1 Answer 1

1

Try:

mysql> DELIMITER //

mysql> CREATE PROCEDURE status_in_timerange
    ->      (
    ->         IN   start_date                     TiMESTAMP, 
    ->         IN   close_date                     TiMESTAMP,
    ->         OUT  total_unsolved                 INT,  
    ->         OUT  today_to_solve                 INT, 
    ->         OUT  days_left_1_3                  INT, 
    ->         OUT  days_left_4_7                  INT, 
    ->         OUT  mt_week                        INT, 
    ->         OUT  overdue_1_3_days               INT, 
    ->         OUT  overdue_4_10_days              INT,      
    ->         OUT  overdue_11_30_days             INT,    
    ->         OUT  overdue_mt_month               INT,    
    ->         OUT  solved_with_delay              INT    
    ->      )
    -> BEGIN 
    ->     SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 
    ->     INTO   total_unsolved , 
    ->            today_to_solve, 
    ->            days_left_1_3, 
    ->            days_left_4_7, 
    ->            mt_week, 
    ->            overdue_1_3_days ,
    ->            overdue_4_10_days,
    ->            overdue_11_30_days,
    ->            overdue_mt_month,
    ->            solved_with_delay
    ->   FROM my_table
    ->   WHERE response_time is null and
    ->         ktimestamp BETWEEN start_date and close_date;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.