|
| 1 | +/* |
| 2 | +
|
| 3 | +Problem Link -> https://leetcode.com/problems/investments-in-2016/description/?envType=study-plan-v2&envId=top-sql-50 |
| 4 | +
|
| 5 | +------------------------------------------------------------- QUESTION ----------------------------------------------------------- |
| 6 | +
|
| 7 | +Table: Insurance |
| 8 | +
|
| 9 | ++-------------+-------+ |
| 10 | +| Column Name | Type | |
| 11 | ++-------------+-------+ |
| 12 | +| pid | int | |
| 13 | +| tiv_2015 | float | |
| 14 | +| tiv_2016 | float | |
| 15 | +| lat | float | |
| 16 | +| lon | float | |
| 17 | ++-------------+-------+ |
| 18 | +pid is the primary key (column with unique values) for this table. |
| 19 | +Each row of this table contains information about one policy where: |
| 20 | +pid is the policyholder's policy ID. |
| 21 | +tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016. |
| 22 | +lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL. |
| 23 | +lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL. |
| 24 | + |
| 25 | +
|
| 26 | +Write a solution to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who: |
| 27 | +have the same tiv_2015 value as one or more other policyholders, and |
| 28 | +are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique). |
| 29 | +Round tiv_2016 to two decimal places. |
| 30 | +
|
| 31 | +The result format is in the following example. |
| 32 | +
|
| 33 | +
|
| 34 | +Example 1: |
| 35 | +
|
| 36 | +Input: |
| 37 | +Insurance table: |
| 38 | ++-----+----------+----------+-----+-----+ |
| 39 | +| pid | tiv_2015 | tiv_2016 | lat | lon | |
| 40 | ++-----+----------+----------+-----+-----+ |
| 41 | +| 1 | 10 | 5 | 10 | 10 | |
| 42 | +| 2 | 20 | 20 | 20 | 20 | |
| 43 | +| 3 | 10 | 30 | 20 | 20 | |
| 44 | +| 4 | 10 | 40 | 40 | 40 | |
| 45 | ++-----+----------+----------+-----+-----+ |
| 46 | +
|
| 47 | +Output: |
| 48 | ++----------+ |
| 49 | +| tiv_2016 | |
| 50 | ++----------+ |
| 51 | +| 45.00 | |
| 52 | ++----------+ |
| 53 | +
|
| 54 | +Explanation: |
| 55 | +The first record in the table, like the last record, meets both of the two criteria. |
| 56 | +The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique. |
| 57 | +
|
| 58 | +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. |
| 59 | +So, the result is the sum of tiv_2016 of the first and last record, which is 45. |
| 60 | +
|
| 61 | +*/ |
| 62 | + |
| 63 | +-- ----------------------------------------------------------- SOLUTION ----------------------------------------------------------- |
| 64 | + |
| 65 | +SELECT ROUND(SUM(tiv_2016), 2) AS tiv_2016 |
| 66 | +FROM Insurance |
| 67 | +WHERE tiv_2015 IN ( |
| 68 | + SELECT tiv_2015 |
| 69 | + FROM Insurance |
| 70 | + GROUP BY tiv_2015 |
| 71 | + HAVING COUNT(*) > 1 |
| 72 | +) |
| 73 | + |
| 74 | +AND (lat, lon) IN ( |
| 75 | + SELECT lat, lon |
| 76 | + FROM Insurance |
| 77 | + GROUP BY lat, lon |
| 78 | + HAVING COUNT(*) = 1 |
| 79 | +) |
| 80 | + |
0 commit comments