Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL sum of sub queries
    primarykey
    data
    text
    <p>I have quite a long query that is causing me some problems. For the first sub-query I keep getting the error: "MySQL server version for the right syntax to use near 'SELECT project.project_total_num_hours_quoted FROM project inner join time_recor' at line 5".</p> <p>The subquery in question is:</p> <pre><code>sum(SELECT project.project_total_num_hours_quoted FROM project inner join time_recording using(project_id) WHERE project.company_id = company.company_id AND project_is_retainer != 1 AND time_recording.time_recording_event_start_datetime &gt;= '2011-01-01' AND time_recording.time_recording_event_stop_datetime &lt;= '2011-03-01' group by project_id ) AS hours_quoted, </code></pre> <p>This returns a set of results. In the larger query I simply want to have the sum.</p> <pre><code>SELECT SUM((unix_timestamp(time_recording.time_recording_event_stop_datetime)-unix_timestamp(time_recording.time_recording_event_start_datetime))/3600) AS total_time, company.company_label, sum(SELECT project.project_total_num_hours_quoted FROM project inner join time_recording using(project_id) WHERE project.company_id = company.company_id AND project_is_retainer != 1 AND time_recording.time_recording_event_start_datetime &gt;= '2011-01-01' AND time_recording.time_recording_event_stop_datetime &lt;= '2011-03-01' group by project_id ) AS hours_quoted, (SELECT SUM(project.project_total_num_hours_quoted) FROM project INNER JOIN time_recording ON project.project_id = time_recording.project_id WHERE time_recording.time_recording_event_start_datetime&gt;='2011-01-01' AND project_is_retainer!=1 AND time_recording.time_recording_event_stop_datetime&lt;='2011-03-01' AND project.company_id!=1 ) AS total_hours_quoted, ( SELECT SUM((unix_timestamp(time_recording.time_recording_event_stop_datetime)-unix_timestamp(time_recording.time_recording_event_start_datetime))/3600) FROM time_recording INNER JOIN project ON time_recording.project_id = project.project_id WHERE project.company_id!=1 AND project_is_retainer!=1 AND time_recording.time_recording_event_start_datetime&gt;='2011-01-01' AND time_recording.time_recording_event_stop_datetime&lt;='2011-03-01' ) AS total_hours FROM time_recording INNER JOIN project ON time_recording.project_id = project.project_id INNER JOIN company ON project.company_id = company.company_id WHERE company.company_id!=1 AND project_is_retainer!=1 AND time_recording.time_recording_event_start_datetime&gt;='2011-01-01' AND time_recording.time_recording_event_stop_datetime&lt;='2011-03-01' GROUP BY company.company_id ORDER BY total_time desc LIMIT 7 </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload