0

I have a projects table and a tasks table. I want a query that gets all projects and the sum of the time_spent columns grouped by project id. So, list all projects and get the total of all the time_spent columns in the tasks table belonging to that project.

With the query below I get the latest added time_spent column and not the sum of all the columns.

SELECT `projects`.`id`, `projects`.`description`, `projects`.`created`,
    `users`.`title`, `users`.`firstname`, `users`.`lastname`,
    `users2`.`title` as assignee_title, `users2`.`firstname` as assignee_firstname,
    `users2`.`lastname` as assignee_lastname,
    (select sum(tasks2.time_spent)
    from tasks tasks2
    where tasks2.id = tasks.id) as project_duration
FROM (`projects`)
LEFT JOIN `users`
ON `users`.`id` = `projects`.`user_id`
LEFT JOIN `users` as users2
ON `users2`.`id` = `projects`.`assignee_id`
LEFT JOIN `tasks` ON `tasks`.`project_id` = `projects`.`id`
GROUP BY `projects`.`id`
ORDER BY `projects`.`created` DESC

projects table: enter image description here

tasks table: enter image description here

3
  • There is no question in this post. Commented May 20 at 14:27
  • Why should I not upload images of code/data/errors? Commented May 20 at 14:27
  • Please ask 1 specific researched non-duplicate question. Either ask re 1 bad query/function with obligatory minimal reproducible example, including why you think it should return something else or are unsure at the 1st subexpression where you don't get what you expect or are stuck, justified by reference to authoritative documentation, or ask about your overall goal giving working parts you can do with justification & a minimal reproducible example--then misunderstood code doesn't belong. But please ask about unexpected behaviour 1st because misconceptions get in the way of your goal. How to Ask Help center Basic questions are faqs. Commented May 20 at 14:28

2 Answers 2

1
SELECT p.*,
   (SELECT SUM(t.time_spent)
    FROM tasks as t
    WHERE t.project_id = p.id) as project_fulltime
FROM projects as p 

Maybe your JOINs don't fetch all the data you need, like users.

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

2 Comments

I didn't post about users as it didn't matter I appended your SUM SQL to what I already have and it worked! Thanks!
they are all left joins so they will have no effect on projects and tasks since there's nothing in the where statement limiting the output based on the left joins.
1

This query should do it for you.

Note, whenever you do a group by you must include every column that you select from or order by. Some MySql installations don't prevent you from doing this, but in the end it results in an incorrect result set.

As well you should never do a query as part of your SELECT statement, known as a sub-query, as it will result in an equal amount of additional queries in relation to the number of rows returned. So if you got 1,000 rows back, it would result in 1,001 queries instead of 1 query.

SELECT 
    p.id,
    p.description,
    p.created,
    u.title,
    u.firstname,
    u.lastname,
    a.title assignee_title,
    a.firstname assignee_firstname,
    a.lastname assignee_lastname,
    SUM(t.time_spent) project_duration
FROM
    projects p

LEFT JOIN
    users u ON
        u.id = p.user_id
LEFT JOIN
    users a ON
        a.id = u.assignee_id
LEFT JOIN
    tasks t ON
        t.project_id = p.id
GROUP BY 
    p.id,
    p.description,
    p.created,
    u.title,
    u.firstname,
    u.lastname,
    a.title,
    a.firstname,
    a.lastname
ORDER BY
    p.created DESC

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.