From 53401384e294b8296f5fc5434b5562482939260b Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:00:16 +0530 Subject: [PATCH 1/2] Create 585. Investments in 2016.sql --- .../585. Investments in 2016.sql | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 LeetCode SQL 50 Solution/585. Investments in 2016.sql diff --git a/LeetCode SQL 50 Solution/585. Investments in 2016.sql b/LeetCode SQL 50 Solution/585. Investments in 2016.sql new file mode 100644 index 0000000..427fb1e --- /dev/null +++ b/LeetCode SQL 50 Solution/585. Investments in 2016.sql @@ -0,0 +1,61 @@ +585. Investments in 2016 +Solved +Medium +Topics +Companies +Hint +SQL Schema +Pandas Schema +Table: Insurance + ++-------------+-------+ +| 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). +Round tiv_2016 to two decimal places. + +The result format is in the following example. + + + +Example 1: + +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 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 is 45. \ No newline at end of file From 97ad079196dd996889fbb63d4551904b9aeba0e2 Mon Sep 17 00:00:00 2001 From: Antim Pal <134076504+iamAntimPal@users.noreply.github.com> Date: Sun, 23 Mar 2025 23:01:05 +0530 Subject: [PATCH 2/2] Update 585. Investments in 2016.sql --- .../585. Investments in 2016.sql | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/LeetCode SQL 50 Solution/585. Investments in 2016.sql b/LeetCode SQL 50 Solution/585. Investments in 2016.sql index 427fb1e..abad2d8 100644 --- a/LeetCode SQL 50 Solution/585. Investments in 2016.sql +++ b/LeetCode SQL 50 Solution/585. Investments in 2016.sql @@ -1,11 +1,5 @@ 585. Investments in 2016 -Solved -Medium -Topics -Companies -Hint -SQL Schema -Pandas Schema +""" Table: Insurance +-------------+-------+ @@ -58,4 +52,19 @@ The first record in the table, like the last record, meets both of the two crite 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 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 is 45. \ No newline at end of file +So, the result is the sum of tiv_2016 of the first and last record, which is 45. + +""" + +WITH + InsuranceWithCounts AS ( + SELECT + tiv_2016, + COUNT(*) OVER(PARTITION by tiv_2015) AS tiv_2015_count, + COUNT(*) OVER(PARTITION by lat, lon) AS city_count + FROM Insurance + ) +SELECT ROUND(SUM(tiv_2016), 2) AS tiv_2016 +FROM InsuranceWithCounts +WHERE tiv_2015_count > 1 + AND city_count = 1; \ No newline at end of file