First and Last Call On the Same Day
First and last call of the day with the same person
Calls(caller_id, recipient_id, call_time). Report users whose first and last call on a day were
with the same other person, counting a user whether they were the caller or the recipient. The earlier
write-up contradicted itself (its Step-3 set listed user 3 as qualifying, but the final output silently dropped it).
The clean approach: unpivot each call into two "(user, partner, time)" rows, then compare each user's
first-of-day partner with their last-of-day partner.
WITH bidir AS ( -- each call seen from both participants' side
SELECT caller_id AS user_id, recipient_id AS partner, call_time FROM Calls
UNION ALL
SELECT recipient_id AS user_id, caller_id AS partner, call_time FROM Calls
),
ends AS (
SELECT DISTINCT user_id,
FIRST_VALUE(partner) OVER (PARTITION BY user_id, DATE(call_time) ORDER BY call_time) AS first_partner,
FIRST_VALUE(partner) OVER (PARTITION BY user_id, DATE(call_time) ORDER BY call_time DESC) AS last_partner
FROM bidir
)
SELECT DISTINCT user_id
FROM ends
WHERE first_partner = last_partner;
Why this is correct
- Unpivot with
UNION ALLso a user is considered on every call they took part in, as caller or recipient — exactly what "regardless of being the caller or the recipient" requires. FIRST_VALUE(... ORDER BY call_time)gives the day's first partner; the same withDESCgives the last. Equal → the day's bookend calls were with the same person.- A day with a single call trivially qualifies (first = last), which is the intended semantics.
Pitfalls
- If two calls share the exact
call_time, the "first/last partner" tie is resolved arbitrarily — add a deterministic tiebreaker (e.g., partner) if your data allows identical timestamps. - Don't join first/last calls back by matching on
call_timeequality (the old approach) — it breaks on duplicate timestamps and silently loses rows.
Takeaways
- Unpivot caller/recipient with
UNION ALLto treat a user symmetrically. FIRST_VALUEascending vs descending over(user, day)gives first/last partner in one pass.
Re-authored for correctness for this guide (the prior worked example contradicted its own output). See also: Window Functions, UNION, Date Functions.
🎯 STRICT STANDOUT: Why / worked / when-not / failure / drills — First and Last Call On the Same Day
Why this concept exists (judgment chain)
Symmetric “user as caller or recipient” requires unpivoting edges with UNION ALL. FIRST_VALUE asc vs desc over (user, day) compares bookend partners without fragile time-equality joins.
Worked example with numbers or traced steps
Calls: A↔B 09:00, A↔C 10:00, A↔B 18:00 same day
bidir: A sees partners B,C,B; first=B last=B → A qualifies.
B sees A,A (if only those); first=last=A → B may qualify.
Single call day: first=last trivially true.
Tie same call_time: add ORDER BY call_time, partner for stability.
When NOT to use / named alternative
Do not join first/last by matching equal timestamps only — duplicate times lose rows. Do not analyze only caller_id (misses recipient-side bookends). Prefer MIN/MAX(call_time) + join back to partner if windows unavailable — more joins, same idea.
Failure / ops fingerprint
Fingerprint: user 3 in intermediate set but missing from output (old bug); timezone DATE(call_time) shifts bookends. Ops: store UTC; deterministic ORDER BY; test single-call and multi-partner days.
Hostile-panel drills (defend the decision)
Q1. Why UNION ALL unpivot?
Model answer: Each call contributes two (user, partner, time) rows so both parties are evaluated symmetrically.
Q2. How do FIRST_VALUE asc and desc help?
Model answer: Asc → first partner of day; desc → last partner; equality means bookends match.
Q3. Single-call day?
Model answer: First partner equals last partner — qualifies under intended semantics.
🤖 Don't fully get this? Learn it with Claude
Stuck on First and Last Call On the Same Day? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Build the mental picture, not memorization.
I just read a lesson on **First and Last Call On the Same Day** (Databases) and want to truly understand it. Explain First and Last Call On the Same Day from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Socratic — adapts to where you're stuck.
Teach me **First and Last Call On the Same Day** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Active recall exposes what you missed.
Quiz me on **First and Last Call On the Same Day** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Intuition + hook + flashcards for long-term memory.
Help me remember **First and Last Call On the Same Day** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.