Search

리트 코드

Game Play Analysis IV - LeetCode
Can you solve this real interview question? Game Play Analysis IV - Table: Activity +--------------+---------+ | Column Name | Type | +--------------+---------+ | player_id | int | | device_id | int | | event_date | date | | games_played | int | +--------------+---------+ (player_id, event_date) is the primary key of this table. This table shows the activity of players of some games. Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device.   Write an SQL query to report the fraction of players that logged in again on the day after the day they first logged in, rounded to 2 decimal places. In other words, you need to count the number of players that logged in for at least two consecutive days starting from their first login date, then divide that number by the total number of players. The query result format is in the following example.   Example 1: Input: Activity table: +-----------+-----------+------------+--------------+ | player_id | device_id | event_date | games_played | +-----------+-----------+------------+--------------+ | 1 | 2 | 2016-03-01 | 5 | | 1 | 2 | 2016-03-02 | 6 | | 2 | 3 | 2017-06-25 | 1 | | 3 | 1 | 2016-03-02 | 0 | | 3 | 4 | 2018-07-03 | 5 | +-----------+-----------+------------+--------------+ Output: +-----------+ | fraction | +-----------+ | 0.33 | +-----------+ Explanation: Only the player with id 1 logged back in after the first day he had logged in so the answer is 1/3 = 0.33
select round(count(distinct if(datediff(next_event_date, event_date) <= 1,player_id,null)) / (select count(distinct player_id) from Activity),2) as fraction from ( select *, lead(event_date,1) over(partition by player_id order by event_date) as next_event_date from Activity ) sub;
SQL
복사
→ 오답.. 왜 틀렸을까..
틀린 이유 : 문제의 의미를 잘 이해하지 못함
핵심 솔루션은 맨 처음 로그인한 이후의 재방문을 했는지 안했는지를 보는 것이라는 점을 유의해야 한다!!!
따라서, 모든 event_date 와 그 다음 방문 event_date 의 차이가 1이 되는 것을 찾는 것이 아니라 반드시 첫번째 방문 이후의 재방문의 날짜 차이가 1이 되는 것을 찾아줘야 한다.
SELECT ROUND(COUNT(t2.player_id)/COUNT(t1.player_id),2) AS fraction FROM (SELECT player_id, MIN(event_date) AS first_login FROM Activity GROUP BY player_id) t1 LEFT JOIN Activity t2 ON t1.player_id = t2.player_id AND t1.first_login = t2.event_date - 1
SQL
복사
select distinct name from Employee where id in ( select managerId from Employee group by managerId having count(*) >= 5 )
SQL
복사
Investments in 2016 - LeetCode
Can you solve this real interview question? Investments in 2016 - Table: Insurance +-------------+-------+ | Column Name | Type | +-------------+-------+ | pid | int | | tiv_2015 | float | | tiv_2016 | float | | lat | float | | lon | float | +-------------+-------+ pid is the primary key column for this table. Each row of this table contains information about one policy where: pid is the policyholder's policy ID. tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016. lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL. lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.   Write an SQL query to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who: * have the same tiv_2015 value as one or more other policyholders, and * are not located in the same city like any other policyholder (i.e., the (lat, lon) attribute pairs must be unique). Round tiv_2016 to two decimal places. The query result format is in the following example.   Example 1: Input: Insurance table: +-----+----------+----------+-----+-----+ | pid | tiv_2015 | tiv_2016 | lat | lon | +-----+----------+----------+-----+-----+ | 1 | 10 | 5 | 10 | 10 | | 2 | 20 | 20 | 20 | 20 | | 3 | 10 | 30 | 20 | 20 | | 4 | 10 | 40 | 40 | 40 | +-----+----------+----------+-----+-----+ Output: +----------+ | tiv_2016 | +----------+ | 45.00 | +----------+ Explanation: The first record in the table, like the last record, meets both of the two criteria. The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique. The second record does not meet any of the two criteria. Its tiv_2015 is not like any other policyholders and its location is the same as the third record, which makes the third record fail, too. So, the result is the sum of tiv_2016 of the first and last record, which is 45.
SELECT ROUND(SUM(TIV_2016),2) AS TIV_2016 FROM Insurance a WHERE EXISTS (SELECT * FROM Insurance WHERE PID <> a.PID AND TIV_2015 = a.TIV_2015) -- pid 가 다르고 tiv_2015는 같으면서 AND NOT EXISTS (SELECT * FROM Insurance WHERE PID <> a.PID AND (LAT,LON) = (a.LAT,a.LON)); -- pid가 다르지만 lat, lon이 같은 행은 제외하면서
SQL
복사
select id ,count(*) num from (select requester_id as id from RequestAccepted union all select accepter_id as id from RequestAccepted ) as a group by id order by num desc limit 1;
SQL
복사