Skip to content

Commit 5340138

Browse files
committed
Create 585. Investments in 2016.sql
1 parent 39d9bdb commit 5340138

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
585. Investments in 2016
2+
Solved
3+
Medium
4+
Topics
5+
Companies
6+
Hint
7+
SQL Schema
8+
Pandas Schema
9+
Table: Insurance
10+
11+
+-------------+-------+
12+
| Column Name | Type |
13+
+-------------+-------+
14+
| pid | int |
15+
| tiv_2015 | float |
16+
| tiv_2016 | float |
17+
| lat | float |
18+
| lon | float |
19+
+-------------+-------+
20+
pid is the primary key (column with unique values) for this table.
21+
Each row of this table contains information about one policy where:
22+
pid is the policyholder's policy ID.
23+
tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
24+
lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
25+
lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
26+
27+
28+
Write a solution to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who:
29+
30+
have the same tiv_2015 value as one or more other policyholders, and
31+
are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique).
32+
Round tiv_2016 to two decimal places.
33+
34+
The result format is in the following example.
35+
36+
37+
38+
Example 1:
39+
40+
Input:
41+
Insurance table:
42+
+-----+----------+----------+-----+-----+
43+
| pid | tiv_2015 | tiv_2016 | lat | lon |
44+
+-----+----------+----------+-----+-----+
45+
| 1 | 10 | 5 | 10 | 10 |
46+
| 2 | 20 | 20 | 20 | 20 |
47+
| 3 | 10 | 30 | 20 | 20 |
48+
| 4 | 10 | 40 | 40 | 40 |
49+
+-----+----------+----------+-----+-----+
50+
Output:
51+
+----------+
52+
| tiv_2016 |
53+
+----------+
54+
| 45.00 |
55+
+----------+
56+
Explanation:
57+
The first record in the table, like the last record, meets both of the two criteria.
58+
The tiv_2015 value 10 is the same as the third and fourth records, and its location is unique.
59+
60+
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.
61+
So, the result is the sum of tiv_2016 of the first and last record, which is 45.

0 commit comments

Comments
 (0)