leetcode512. gameplay analysis II (SQL)

Table: Activity

+ --------- + -------------- +
| Column the Name | Type |
+ -------------- + - + -------
| player_id | int |
| DEVICE_ID | int |
| event_date | DATE |
| games_played | int |
+ ------ + -------------- + ---
(player_id, event_date) are the two primary key of
this table shows some of the game player's game activities
each line is a device prior to use to log out and play more games (might one day 0) record player
please write a SQL query, describing each player the first landing of the device name

Query results format in the following example:

Activity table:
+-----------+-----------+------------+--------------+
| player_id | device_id | event_date | games_played |
+-----------+-----------+------------+--------------+
| 1         | 2         | 2016-03-01 | 5            |
| 1         | 2         | 2016-05-02 | 6            |
| 2         | 3         | 2017-06-25 | 1            |
| 3         | 1         | 2016-03-02 | 0            |
| 3         | 4         | 2018-07-03 | 5            |
+-----------+-----------+------------+--------------+

Result table:
+-----------+-----------+
| player_id | device_id |
+-----------+-----------+
| 1         | 2         |
| 2         | 3         |
| 3         | 1         |
+-----------+-----------+

Ideas: nested queries, for each player, to identify the earliest time, then you can select the corresponding device_id According to the most between the player and id.

select a.player_id as 'player_id',a.device_id as 'device_id'
from activity as a
where a.event_date=(select min(b.event_date) from activity as b where a.player_id=b.player_id);

 

Published 552 original articles · won praise 10000 + · views 1.32 million +

Guess you like

Origin blog.csdn.net/hebtu666/article/details/104322325