To calculate the total cost for events that ended per day, you would need a way to associate a cost with an event. However, your current table schema does not include explicit cost information. If there's an implicit way to determine the cost (for example, if different activities have different fixed costs), you need to define how these costs should be calculated.
Without specific cost details, I can only show you an SQL query for the total number of events that ended each day or the total duration of events instead of cost. If event duration somehow correlates with cost, you could use this information as a proxy for total cost. Below is an example of how to calculate the total duration of events that ended per day:
SELECT
strftime('%Y-%m-%d', e_end.timestamp) as day,
SUM(e_end.timestamp - e_start.timestamp) as total_duration
FROM
(SELECT
idcase,
MIN(timestamp) as timestamp
FROM events_log
GROUP BY idcase) as e_start
JOIN
(SELECT
idcase,
MAX(timestamp) as timestamp
FROM events_log
GROUP BY idcase) as e_end
ON e_start.idcase = e_end.idcase
GROUP BY day;
In the above query:
- Subquery
e_start finds the start timestamp for each case.
- Subquery
e_end finds the end timestamp for each case.
- The
JOIN is made on idcase to correlate the start and end events of the cases.
- The difference in timestamps (
e_end.timestamp - e_start.timestamp) is summed for events grouped by day, as determined by strftime('%Y-%m-%d', e_end.timestamp).
Let's consider you have cost information that assigns a cost value to each activity or resource. You could then modify the query to calculate the cost. Here’s an example query, assuming there is a fixed cost per unit of time (which we will fictitiously call unit_cost):
SELECT
strftime('%Y-%m-%d', e.timestamp) as day,
SUM((julianday(e_end.timestamp) - julianday(e_start.timestamp)) * 24 * unit_cost) as total_cost
FROM
(SELECT idcase, MIN(timestamp) as timestamp FROM events_log GROUP BY idcase) as e_start
JOIN
(SELECT idcase, MAX(timestamp) as timestamp FROM events_log GROUP BY idcase) as e_end
ON e_start.idcase = e_end.idcase
JOIN
events_log as e
ON e.idcase = e_end.idcase
GROUP BY day;
This query calculates the difference in Julian days between the start and end timestamps, then assumes there's a constant unit_cost per hour, and calculates the total cost per day.
Please adjust the query as per your specific cost determination method. If you have different costs for different activities or resources, you'll need to join those cost values from another table or include conditional logic in your query.