SELECT
customer_id, customer_type_id, COUNT(id) AS total_calls,
MAX(created_at) AS created_at,
(
SELECT
details
FROM call_logs AS c2
WHERE c2.customer_id = c1.customer_id AND c2.customer_type_id = c1.customer_type_id
ORDER BY created_at DESC LIMIT 1
) AS latest_details
FROM call_logs c1
GROUP BY customer_id, customer_type_id
Can this be done with join instead of subquery?
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Yes, the query can be rewritten using JOINs instead of a correlated subquery. Here's how you can achieve this using a window function to efficiently get the latest details per group:
sql
SELECT
grouped.customer_id,
grouped.customer_type_id,
grouped.total_calls,
grouped.latest_created_at,
latest.details
FROM (
-- Aggregate total calls and latest created_at per customer
SELECT
customer_id,
customer_type_id,
COUNT(id) AS total_calls,
MAX(created_at) AS latest_created_at
FROM call_logs
GROUP BY customer_id, customer_type_id
) AS grouped
INNER JOIN (
-- Get the latest details per customer using row numbering
SELECT
customer_id,
customer_type_id,
details,
ROW_NUMBER() OVER (
PARTITION BY customer_id, customer_type_id
ORDER BY created_at DESC
) AS rn
FROM call_logs
) AS latest
ON latest.customer_id = grouped.customer_id
AND latest.customer_type_id = grouped.customer_type_id
AND latest.rn = 1;
Key Improvements:
Separate Aggregation and Latest Details:
First, calculate total_calls and latest_created_at in the grouped subquery.
Then, use ROW_NUMBER() to rank rows by created_at (descending) per customer_id and customer_type_id. This ensures you only select the latest row (rn = 1).
Avoids Correlated Subquery:
The original query uses a correlated subquery in the SELECT clause, which can be inefficient for large datasets. This version uses a JOIN and a window function, which is typically faster and more scalable.
Handles Ties Gracefully:
If multiple rows share the same created_at, ROW_NUMBER() ensures a deterministic choice (you can add additional tie-breakers like id DESC in the ORDER BY clause if needed).
Notes:
Window Function Support: This solution assumes your database supports ROW_NUMBER() (e.g., PostgreSQL, SQL Server, MySQL 8.0+, Oracle).
Without Window Functions: If your database lacks window functions, you’d need to use a self-join with a derived table that finds the maximum created_at per group, but this approach may return duplicates if multiple rows share the same created_at.
Alternative (No Window Functions):
If window functions aren’t available, use a self-join to find the latest created_at and join back to get details:
sql
SELECT
c1.customer_id,
c1.customer_type_id,
COUNT(c1.id) AS total_calls,
MAX(c1.created_at) AS latest_created_at,
c2.details
FROM call_logs c1
INNER JOIN (
-- Find the latest created_at per customer
SELECT customer_id, customer_type_id, MAX(created_at) AS latest_created_at
FROM call_logs
GROUP BY customer_id, customer_type_id
) AS latest
ON c1.customer_id = latest.customer_id
AND c1.customer_type_id = latest.customer_type_id
INNER JOIN call_logs c2
ON c2.customer_id = latest.customer_id
AND c2.customer_type_id = latest.customer_type_id
AND c2.created_at = latest.latest_created_at
GROUP BY c1.customer_id, c1.customer_type_id, c2.details;
Caveat: This may return multiple rows per customer_id/customer_type_id if there are ties in created_at. To mimic LIMIT 1, you’d need additional logic (e.g., DISTINCT ON or filtering with id).