3

i am trying to run this SQL Query:

SELECT avg(response_seconds) as s FROM 
    ( select time_to_sec( timediff( from_unixtime( floor( UNIX_TIMESTAMP(u.datetime)/60 )*60 ), u.datetime) ) ) as response_seconds 
    FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
    WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
    GROUP BY t.ticketnumber) 
    AS r 

but i am seeing this 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 'FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber WHE' at line 3

and i cannot work out where the error is in the query

8
  • What SQL? SQL Server? Update your tags. Commented Apr 28, 2016 at 17:12
  • edited question :) Commented Apr 28, 2016 at 17:13
  • 6
    Looks like you closed ) too early before as response_seconds. That belongs inside the (select...) but you have it outside. The correct syntax to use near FROM means you must look immediately before that to find the issue. Commented Apr 28, 2016 at 17:16
  • 1
    You have 2 FROMs - One from the nested query and the second from tickets, you need to JOIN tickets to the query Commented Apr 28, 2016 at 17:16
  • 1
    You have 6 opening (, which should be finally closed after GROUP BY, but the 6th closing ) occurs just before as response_seconds instead. Commented Apr 28, 2016 at 17:18

4 Answers 4

2

Remove the ) just before as response_seconds

SELECT avg(response_seconds) as s FROM 
    ( select time_to_sec( timediff( from_unixtime( floor( UNIX_TIMESTAMP(u.datetime)/60 )*60 ), u.datetime) ) as response_seconds 
      FROM tickets t 
         JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
      WHERE u.type = 'update' 
        and t.customer = 'Y' 
        and DATE(u.datetime) = '2016-04-18' 
      GROUP BY t.ticketnumber
    ) AS r 

You had to many closing brackets on that calculation which had the effect of closing the sub select to early.

Sign up to request clarification or add additional context in comments.

3 Comments

But then you have FROM response_seconds and FROM tickets?
Its a sub query, it need a FROM
Oh, now after the aligning, I see you're correct. +1
1

) the one more extra parenthesis in the ) ) as response_seconds causing the problem, removing that will solve the problem. For better readability I aligned the code:

SELECT avg(response_seconds) AS s 
FROM 
( 
    SELECT 
        time_to_sec( 
            timediff( 
                from_unixtime( 
                    floor( 
                            UNIX_TIMESTAMP(u.datetime)/60 
                        )*60 
                ), u.datetime
            )   -- ) the one more extra parenthesis causing the problem
        ) as response_seconds 
    FROM tickets t 
    JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
    WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
    GROUP BY t.ticketnumber
) AS r 

Comments

0

It seems that you have an extra ')' here "tetime) ) ) <-- as response_seco"


SELECT avg(response_seconds) as s
FROM (
      select time_to_sec( timediff( from_unixtime( floor( UNIX_TIMESTAMP(u.datetime)/60 )*60 ), u.datetime) ) as response_seconds 
      FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
      WHERE u.type = 'update' and t.customer = 'Y' and DATE(u.datetime) = '2016-04-18' 
      GROUP BY t.ticketnumber
    ) AS r 

Comments

-1

Count your parentheses. You're essentially doing this:

SELECT some_column as s
FROM some_sub_select as response_seconds 
FROM tickets t JOIN ticket_updates u ON t.ticketnumber = u.ticketnumber 
....

You can't have two FROM clauses in the same query.

Maybe you have mis-aligned parentheses? Maybe you meant to do a JOIN instead of a second FROM? It's difficult to tell from what appears to be an incomplete overall query.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.