Skip to content

Commit 786e256

Browse files
authored
Merge pull request #52 from iamAntimPal/Branch-1
Branch 1
2 parents 17fabaf + 97ad079 commit 786e256

File tree

1 file changed

+70
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)