File tree Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Expand file tree Collapse file tree 2 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ Have a good contributing!
8383 - [ 184. Department Highest Salary] ( ./leetcode/medium/184.%20Department%20Highest%20Salary.sql )
8484 - [ 550. Game Play Analysis IV] ( ./leetcode/medium/550.%20Game%20Play%20Analysis%20IV.sql )
8585 - [ 570. Managers with at Least 5 Direct Reports] ( ./leetcode/medium/570.%20Managers%20with%20at%20Least%205%20Direct%20Reports.sql )
86+ - [ 585. Investments in 2016] ( ./leetcode/medium/585.%20Investments%20in%202016.sql )
8687 - [ 602. Friend Requests II: Who Has the Most Friends] ( ./leetcode/medium/602.%20Friend%20Requests%20II:%20Who%20Has%20the%20Most%20Friends.sql )
8788 - [ 626. Exchange Seats] ( ./leetcode/medium/626.%20Exchange%20Seats.sql )
8889 - [ 1045. Customers Who Bought All Products] ( ./leetcode/medium/1045.%20Customers%20Who%20Bought%20All%20Products.sql )
Original file line number Diff line number Diff line change 1+ /*
2+ Question 585. Investments in 2016
3+ Link: https://leetcode.com/problems/investments-in-2016/description/?envType=study-plan-v2&envId=top-sql-50
4+
5+ Table: Insurance
6+
7+ +-------------+-------+
8+ | Column Name | Type |
9+ +-------------+-------+
10+ | pid | int |
11+ | tiv_2015 | float |
12+ | tiv_2016 | float |
13+ | lat | float |
14+ | lon | float |
15+ +-------------+-------+
16+ pid is the primary key (column with unique values) for this table.
17+ Each row of this table contains information about one policy where:
18+ pid is the policyholder's policy ID.
19+ tiv_2015 is the total investment value in 2015 and tiv_2016 is the total investment value in 2016.
20+ lat is the latitude of the policy holder's city. It's guaranteed that lat is not NULL.
21+ lon is the longitude of the policy holder's city. It's guaranteed that lon is not NULL.
22+
23+
24+ Write a solution to report the sum of all total investment values in 2016 tiv_2016, for all policyholders who:
25+
26+ have the same tiv_2015 value as one or more other policyholders, and
27+ are not located in the same city as any other policyholder (i.e., the (lat, lon) attribute pairs must be unique).
28+ Round tiv_2016 to two decimal places.
29+ */
30+
31+ SELECT ROUND(SUM (i .tiv_2016 )::numeric , 2 ) AS tiv_2016
32+ FROM Insurance AS i
33+ WHERE (i .lat , i .lon ) IN (
34+ SELECT
35+ i1 .lat ,
36+ i1 .lon
37+ FROM Insurance AS i1
38+ GROUP BY i1 .lat , i1 .lon
39+ HAVING COUNT (1 ) = 1
40+ ) AND i .tiv_2015 IN (
41+ SELECT i2 .tiv_2015
42+ FROM Insurance AS i2
43+ GROUP BY i2 .tiv_2015
44+ HAVING COUNT (1 ) > 1
45+ )
You can’t perform that action at this time.
0 commit comments