To identify cases with double payment, we'll need to check for the presence of two "payment" activities within the same case. The SQLite SQL query can use a combination of aggregation and the HAVING clause to filter out cases that have two or more payment activities.
Assuming the "payment" activity is literally recorded as "payment" in your activity column, and you want cases which have multiple payment events logged, the SQL query would look something like this:
SELECT idcase, COUNT(activity) as payment_count
FROM events_log
WHERE activity = 'payment'
GROUP BY idcase
HAVING COUNT(activity) > 1;
This would give you a list of cases (idcase) with the number of recorded payment activities where that count is greater than one, indicating double or multiple payments.
However, if you want cases along with their variants (concatenation of the activities) and the duration between the first and the last event, the query is a bit more complex and requires a subquery:
WITH case_activities AS (
SELECT
idcase,
GROUP_CONCAT(activity, '->') AS variant,
strftime('%s', MAX(timestamp)) - strftime('%s', MIN(timestamp)) AS duration
FROM
events_log
GROUP BY
idcase
),
payment_counts AS (
SELECT
idcase,
COUNT(*) AS payment_count
FROM
events_log
WHERE
activity = 'payment'
GROUP BY
idcase
)
SELECT
ca.idcase,
ca.variant,
ca.duration,
pc.payment_count
FROM
payment_counts pc
JOIN
case_activities ca ON pc.idcase = ca.idcase
WHERE
pc.payment_count > 1;
In this query:
-
The case_activities common table expression (CTE) creates a subset of data with a concatenated string of activities for each case, and the duration in seconds between the first and last events of each case.
-
The payment_counts CTE aggregates the payment activities and calculates a count of 'payment' activities for each case.
-
The main query joins these two CTEs on idcase and filters the results to only include cases where the payment count is greater than one, indicating double payments.
-
The GROUP_CONCAT function is used to concatenate the activities to form variants, and the strftime function is used alongside MAX and MIN to calculate the duration.
Remember to replace 'payment' with the appropriate string that represents a payment activity in your events_log table if it is different. Additionally, this query assumes that the timestamp field is in a format compatible with SQLite's strftime function. If your timestamps are stored in a different format, you may need to adjust the use of strftime accordingly.