Conditional SQL Query Which Sums ALL TimeEntires of Related Task if any TimeEntries Updated in Last 10 Days [closed]

You can use a having clause:

select taskassignmentid, sum(userassignment_hourlyrate * roundedhours) as spent
from mytable
group by taskassignmentid
having max(updatedat) >= current_date - interval 10 day

This computes the overal spent for each taskassignmentid whose latest updatedat is less than 10 days old.

Notes:

  • I fixed the original code to make it a valid aggregation query; the columns in the select and group by clause must be consistent: every non-aggregated column in the select clause must be repeated in the group by clause (left alone functionaly-dependent columns – but your question does not mention that)

  • don’t use single quotes for identifiers (such as column aliases)! They should be used for literal strings only; use backticks for identifiers, or better yet, use identifiers that don’t need to be quoted

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top