DIV of Failed Queries: Every derived table must have its Own alias

Asked

Viewed 29 times

0

In a table, I have two fields: entry_time and exit_time. I want to subtract each record (Exit - entry) to find the wait of each client, add all the waits, and take an average of that sum by the number of clients.

There are some conditions that are already in the query but in essence what I want is:

sum(exit_time - entry_time) div count(*)

I have a query that brings me a display in the two generated fields that I need to do DIV():

SELECT SUM(espera) soma, COUNT(*) as totalregistros FROM 
   (SELECT timestampdiff(minute,queue_entry_time,queue_exit_time) espera 
       FROM queue_element where status='done') soma;

        +------+----------------+
        | soma | totalregistros |
        +------+----------------+
        |   45 |              8 |
        +------+----------------+

If I try to divide one field by another I get the following error:

SELECT soma div totalregistros from (
   SELECT SUM(espera) soma, COUNT(*) as totalregistros 
      FROM (SELECT timestampdiff(minute,queue_entry_time,queue_exit_time) espera 
         FROM queue_element where status='done') soma);

ERROR 1248 (42000): Every derived table must have its own alias

However I cannot see which subselect is without alias. Remember that I cannot use procedures or temporary table in my scenario.

Could someone point out my mistake? Thank you!

1 answer

0


Your error is missing declaring one as XXX at the end

SELECT soma div totalregistros from (
   SELECT SUM(espera) soma, COUNT(*) as totalregistros 
      FROM (SELECT timestampdiff(minute,queue_entry_time,queue_exit_time) espera 
         FROM queue_element where status='done') soma) as Resultado;

But to make it simple, you tried it this way ?

SELECT (SUM(espera) div COUNT(*)) as resultado FROM 
   (SELECT timestampdiff(minute,queue_entry_time,queue_exit_time) espera 
       FROM queue_element where status='done') as reFinal;
  • Thank you very much! It worked perfectly!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.