A temporary email service gives every address a fixed lifetime: each address expires exactly 5 hours after it is created. The service also logs every message delivered to an address.
For each temporary email, report its status as of the reference time 2024-01-15 12:00:00 and summarize the messages it received while it was still valid: how many there were and when the last one arrived. Every email must appear in the output, including emails that never received a qualifying message.
Input Table(s)
temp_emails
| Column | Type | Meaning |
|---|
| email_id | INT | Primary key |
| email_address | VARCHAR | The temporary address (unique) |
| created_at | TIMESTAMP | When the address was created |
| expires_at | TIMESTAMP | When the address stops being valid, exactly 5 hours after created_at |
Primary key: email_id. email_address is unique.
messages
| Column | Type | Meaning |
|---|
| message_id | INT | Primary key |
| email_id | INT | The address that received the message, foreign key to temp_emails.email_id |
| received_at | TIMESTAMP | When the message arrived |
Primary key: message_id. Every email_id references an existing row in temp_emails.
Output Contract
One row per row of temp_emails, with exactly these columns in this order. The mixed-case names are part of the contract, so quote them in PostgreSQL.
| Column | Value |
|---|
| email_id | temp_emails.email_id |
| Email | temp_emails.email_address |
| CreatedAt | temp_emails.created_at |
| ExpiresAt | temp_emails.expires_at |
| Status | 'Active' when expires_at is later than the reference time, otherwise 'Expired' |
| MessageCount | Number of qualifying messages, 0 when there are none |
| LastMessageAt | Latest received_at among qualifying messages, NULL when there are none |
A message qualifies when it arrived while its email was valid and no later than the reference time, that is created_at <= received_at < expires_at and received_at <= '2024-01-15 12:00:00'.
Order the rows by expires_at descending, then email_id ascending. No rounding is involved.
Example
temp_emails
| email_id | email_address | created_at | expires_at |
|---|
| 1 | a1@tmp.io | 2024-01-15 05:00:00 | 2024-01-15 10:00:00 |
| 2 | b2@tmp.io | 2024-01-15 09:30:00 | 2024-01-15 14:30:00 |
| 3 | c3@tmp.io | 2024-01-15 07:00:00 | 2024-01-15 12:00:00 |
| 4 | d4@tmp.io | 2024-01-15 11:00:00 | 2024-01-15 16:00:00 |
messages
| message_id | email_id | received_at |
|---|
| 101 | 1 | 2024-01-15 06:15:00 |
| 102 | 1 | 2024-01-15 09:59:00 |
| 103 | 1 | 2024-01-15 10:30:00 |
| 104 | 2 | 2024-01-15 10:00:00 |
| 105 | 2 | 2024-01-15 12:45:00 |
| 106 | 3 | 2024-01-15 11:59:59 |
| 107 | 3 | 2024-01-15 12:00:00 |
Expected result:
| email_id | Email | CreatedAt | ExpiresAt | Status | MessageCount | LastMessageAt |
|---|
| 4 | d4@tmp.io | 2024-01-15 11:00:00 | 2024-01-15 16:00:00 | Active | 0 | NULL |
| 2 | b2@tmp.io | 2024-01-15 09:30:00 | 2024-01-15 14:30:00 | Active | 1 | 2024-01-15 10:00:00 |
| 3 | c3@tmp.io | 2024-01-15 07:00:00 | 2024-01-15 12:00:00 | Expired | 1 | 2024-01-15 11:59:59 |
| 1 | a1@tmp.io | 2024-01-15 05:00:00 | 2024-01-15 10:00:00 | Expired | 2 | 2024-01-15 09:59:00 |
Email 1 expired at 10:00. Messages 101 and 102 arrived inside its window, while 103 arrived at 10:30, after expiry, so it is ignored even though it is earlier than the reference time. Email 2 is still active; message 104 counts and message 105 arrived after the reference time. Email 3 expires exactly at the reference time, so it is Expired, and message 107 arrived at the moment of expiry, so only 106 counts. Email 4 has no messages at all.
Constraints and Clarifications
-
temp_emails
has at most 10,000 rows and
messages
has at most 100,000 rows.
-
expires_at
is always exactly 5 hours after
created_at
.
-
No column in either table is NULL.
-
An email whose
expires_at
equals the reference time is
Expired
. A message received exactly at
expires_at
does not qualify.
-
Messages received after the reference time are ignored, even for an email that is still active.
-
Messages received after an email expired, or timestamped before it was created, are ignored.
-
Several messages can share the same
received_at
; each one counts.
-
Use the fixed reference time
2024-01-15 12:00:00
, never the current clock.