585. Investments in 2016
Problem Description
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_2015value 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).
Round tiv_2016 to two decimal places.
The query result format is in the following example.
Example:
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 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.
Solution
MySQL Solution
SELECT ROUND(SUM(tiv_2016), 2) as tiv_2016
FROM Insurance i1
WHERE tiv_2015 IN (
SELECT tiv_2015
FROM Insurance i2
WHERE i2.pid != i1.pid
)
AND NOT EXISTS (
SELECT 1
FROM Insurance i3
WHERE i3.pid != i1.pid
AND i3.lat = i1.lat
AND i3.lon = i1.lon
);
Alternative Solution:
WITH DupTiv AS (
SELECT tiv_2015
FROM Insurance
GROUP BY tiv_2015
HAVING COUNT(*) > 1
),
UniqueLoc AS (
SELECT lat, lon
FROM Insurance
GROUP BY lat, lon
HAVING COUNT(*) = 1
)
SELECT ROUND(SUM(i.tiv_2016), 2) as tiv_2016
FROM Insurance i
JOIN DupTiv d ON i.tiv_2015 = d.tiv_2015
JOIN UniqueLoc u ON i.lat = u.lat AND i.lon = u.lon;
PostgreSQL Solution
SELECT ROUND(SUM(tiv_2016)::numeric, 2) as tiv_2016
FROM Insurance i1
WHERE tiv_2015 IN (
SELECT tiv_2015
FROM Insurance i2
WHERE i2.pid != i1.pid
)
AND NOT EXISTS (
SELECT 1
FROM Insurance i3
WHERE i3.pid != i1.pid
AND i3.lat = i1.lat
AND i3.lon = i1.lon
);
MS SQL Server Solution
SELECT CAST(ROUND(SUM(tiv_2016), 2) AS DECIMAL(10,2)) as tiv_2016
FROM Insurance i1
WHERE tiv_2015 IN (
SELECT tiv_2015
FROM Insurance i2
WHERE i2.pid != i1.pid
)
AND NOT EXISTS (
SELECT 1
FROM Insurance i3
WHERE i3.pid != i1.pid
AND i3.lat = i1.lat
AND i3.lon = i1.lon
);
Oracle Solution
SELECT ROUND(SUM(tiv_2016), 2) as tiv_2016
FROM Insurance i1
WHERE tiv_2015 IN (
SELECT tiv_2015
FROM Insurance i2
WHERE i2.pid != i1.pid
)
AND NOT EXISTS (
SELECT 1
FROM Insurance i3
WHERE i3.pid != i1.pid
AND i3.lat = i1.lat
AND i3.lon = i1.lon
);
Approach Explanation
The solution uses subqueries and EXISTS clause:
- Key Insights:
- Duplicate tiv_2015
- Unique location
- Condition combination
- Result rounding
- Query Steps:
- Check tiv_2015
- Verify location
- Sum tiv_2016
- Round result
Implementation Details:
- Subquery usage
- EXISTS clause
- Condition handling
- Result formatting
Optimization Insights:
- Index usage
- Subquery efficiency
- Condition order
- Result aggregation
Edge Cases:
- No duplicates
- All same location
- Empty table
- Single record