GuilinDev

Lc0585

05 August 2008

585. Investments in 2016

+————-+——-+ | 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. lon is the longitude of the policy holder’s city.

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_2015 value as one or more other policyholders, and are not located in the same city like 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.

1
2
3
4
5
6
7
8
SELECT ROUND(SUM(TIV_2016),2) AS TIV_2016
FROM
(SELECT *,
COUNT(*) OVER(PARTITION BY TIV_2015) AS CNT1,
COUNT(*) OVER(PARTITION BY LAT, LON) AS CNT2
FROM INSURANCE
) AS TBL
WHERE CNT1 > =2 AND CNT2 =1