---
# System prepended metadata

title: "\n"

---

```python


```

user_id: int, enter_timestamp: timestamp, left_timestamp: timestamp

We recorded the time when every visitor entered and left a party and stored it in a database.
We would like to write code to compute when the party reached the peak of visitors. You are given the data with the following schema:

user_id: int, enter_timestamp: timestamp, left_timestamp: timestamp

Sample of data:

user_id enter_timestamp left_timestamp
1 2020-09-24 18:00:00 2020-09-24 22:00:00
2 2020-09-24 19:00:00 2020-09-24 21:00:00
3 2020-09-24 23:00:00 2020-09-25 01:00:00
4 2020-09-24 20:00:00 2020-09-25 02:00:00
5 2020-09-24 19:00:00 2020-09-24 22:00:00
... ... ...


We ask you to write logic that computes the timestamp when there was the maximum number of visitors at the party.
The method returns the timestamp and the number of visitors.

user_id enter_timestamp left_timestamp
1 2020-09-24 18:00:00 2020-09-24 22:00:00

2 2020-09-24 19:00:00 2020-09-24 21:00:00
3 2020-09-24 23:00:00 2020-09-25 01:00:00
4 2020-09-24 20:00:00 2020-09-25 02:00:00
5 2020-09-24 19:00:00 2020-09-24 22:00:00
... ... ...

timestamp                value      current_amount
2020-09-24 18:00:00        1            1
2020-09-24 19:00:00        1            2
2020-09-24 21:00:00       -1            1
2020-09-24 22:00:00       -1            0


```sql
WITH party_events AS (
 SELECT user_id,
        enter_timestamp AS timestamp,
        1 AS visitor_change
 FROM parties
    UNION
 SELECT user_id,
        left_timestamp AS timestamp,
        -1 AS visitor_change
 FROM parties
), current_amounts AS (
    SELECT 
        timestamp,
        SUM(visitor_change) OVER (ORDER BY timestamp ASC, visitor_change DESC) AS current_amount
    FROM party_events
)
SELECT 
    timestamp, 
    current_amount 
FROM current_amounts 
WHERE 
current_amount = (SELECT MAX(current_amount) FROM current_amounts);
```

