Now that I have the additional endpoint data working to track an individual painting’s progress, I need a query to return the total-duration from the ‘painting_status’, “start” to each day’s “in progress” and ending with the status “completed”. Here is the current basic structure from each of my two tables. NOTE: * denotes entries created via the IOS Shortcuts image-upload to the endpoint ‘/uploads’.
<progress>
id
uuid *
timestamp_for_DB *
image
working_title
final_title
painting_status (start, in progress, completed) *
<time_data>
id
uuid *
timestamp_for_DB *
created_at
the_date
time_in
time_out
duration
entry
notes
To calculate the total duration from “start” to “in progress” days and ending with “completed” for an individual painting, you can use a SQL query that joins the progress and time_data tables on the uuid field, and then aggregates the duration based on the painting’s lifecycle defined in painting_status.
This query will:
progress table.duration in the time_data table for all entries within that date range for each painting.Here’s an example query that achieves this:
SELECT
p.working_title,
p.final_title,
SUM(TIME_TO_SEC(TIMEDIFF(td.time_out, td.time_in))) AS total_seconds
FROM
progress p
JOIN
time_data td ON p.uuid = td.uuid
WHERE
p.uuid IN (
SELECT uuid FROM progress WHERE painting_status = 'start'
)
AND td.the_date >= (
SELECT timestamp_for_DB FROM progress WHERE uuid = p.uuid AND painting_status = 'start'
)
AND td.the_date <= COALESCE(
(
SELECT timestamp_for_DB FROM progress WHERE uuid = p.uuid AND painting_status = 'completed'
),
CURDATE() -- Assuming you want to include up to the current date if not completed
)
GROUP BY
p.uuid
SELECT uuid FROM progress WHERE painting_status = 'start' identifies the UUIDs of paintings that have started.progress table and uses it as the lower bound of the date range.COALESCE function), it uses the current date (CURDATE()) as the upper bound.duration column in the time_data table) for all records within this date range.TIME_TO_SEC(TIMEDIFF(td.time_out, td.time_in)) calculates the difference in seconds between time_out and time_in for each entry in time_data, which are then summed up to get total_seconds.time_in and time_out in the time_data table are of a time or datetime type and that duration can be calculated as the difference between these two fields.timestamp_for_DB in your progress table and the_date in your time_data table are compatible date types for comparison.td.the_date >= ... AND td.the_date <= ...) assumes that the_date represents the date portion of your timestamps. Adjust this part if the_date includes time components or if your date range logic differs.Please adjust the field names and logic as necessary to fit your exact database schema and requirements.