+————-+——-+
| Column Name | Type |
+————-+——-+
| pid | int |
| tiv_2015 | float |
| tiv_2016 | float |
| lat | float |
| lon | float |
+————-+——-+
pid is the primary key (column with unique values) 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 a solution 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 as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique).
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 10is 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 is45.## Solution
### Method 1– Group By and Self Join
#### Intuition
We need to sum the 2016 investment values for policies that meet two conditions: their 2015 investment value issharedwith at least one other policy, and their location(lat, lon)is unique among those with the same 2015 value. We use grouping and self-join to filter these policies.#### Approach
1. Group by `tiv_2015` and select those values that appear more than once.2. For these, select policies whose(lat, lon)is unique (i.e., not sharedwith any other policy with the same `tiv_2015`).3. Sum the `tiv_2016`for these policies.#### Code
1
2
3
4
5
6
7
8
SELECT ROUND(SUM(tiv_2016), 2) AS tiv_2016
FROM Insurance
WHERE tiv_2015 IN (
SELECT tiv_2015 FROM Insurance GROUPBY tiv_2015 HAVINGCOUNT(*) >1)
AND (lat, lon) IN (
SELECT lat, lon FROM Insurance GROUPBY lat, lon HAVINGCOUNT(*) =1);
1
2
3
4
5
6
7
8
SELECT ROUND(SUM(tiv_2016)::numeric, 2) AS tiv_2016
FROM Insurance
WHERE tiv_2015 IN (
SELECT tiv_2015 FROM Insurance GROUPBY tiv_2015 HAVINGCOUNT(*) >1)
AND (lat, lon) IN (
SELECT lat, lon FROM Insurance GROUPBY lat, lon HAVINGCOUNT(*) =1);
#### Complexity
-⏰ Time complexity:`O(n)`— Each row is processed a constant number of times for grouping and filtering.-🧺 Space complexity:`O(n)`— For storing intermediate groupings.