Investments in 2016 SQL MySQL Leetcode Solution with SELECT CONCAT

preview_player
Показать описание
Finding Total Investment Value (2016) for Policyholders with Matching Investments in Different Locations
This script calculates the sum of total investment values (tiv_2016) for policyholders in 2016 who:

Share the same tiv_2015 value with at least one other policyholder.
Reside in unique locations (distinct latitude and longitude combinations).
Input:

A table named "Insurance" with the following columns:

pid (int): Unique policy ID (primary key)
tiv_2015 (float): Total investment value in 2015
tiv_2016 (float): Total investment value in 2016
lat (float): Policyholder's city latitude (guaranteed not null)
lon (float): Policyholder's city longitude (guaranteed not null)
Output:

A table with one column named "tiv_2016" containing the rounded sum (to two decimal places) of tiv_2016 values meeting the criteria.

The given SQL query performs a series of operations on an Insurance table. Here's a step-by-step explanation:

Main Query:

sql
Copy code
SELECT sum(tiv_2016) tiv_2016
FROM
Insurance
WHERE tiv_2015 IN
(SELECT tiv_2015 FROM Insurance GROUP BY tiv_2015 HAVING count(*)>1) AND
CONCAT(LAT, LON) IN (SELECT CONCAT(LAT, LON) FROM Insurance GROUP BY LAT, LON HAVING COUNT(*) = 1);
Objective:
The query aims to compute the sum of tiv_2016 values for certain insurance records based on specific conditions.

Conditions:

The tiv_2015 value of the records must be present in a subset of records where tiv_2015 is not unique (appears more than once).
The combination of LAT (latitude) and LON (longitude) values of the records must be unique (appear only once).
Breakdown:
Main Query:
SELECT sum(tiv_2016) tiv_2016:
This part of the query selects the sum of the tiv_2016 column values and gives it an alias tiv_2016.

FROM Insurance:
The data is being queried from the Insurance table.

WHERE tiv_2015 IN (...) AND CONCAT(LAT, LON) IN (...):
The WHERE clause filters the rows based on two conditions:

First Condition:
tiv_2015 IN:
This checks if the tiv_2015 value of a row is in the list of tiv_2015 values returned by the subquery:
sql
Copy code
(SELECT tiv_2015 FROM Insurance GROUP BY tiv_2015 HAVING count(*)>1)
This subquery groups the Insurance table by tiv_2015 and returns those tiv_2015 values which have more than one record (HAVING count(*)>1).
Second Condition:
CONCAT(LAT, LON) IN:
This checks if the concatenated string of LAT and LON values of a row is in the list of concatenated LAT and LON values returned by another subquery:
sql
Copy code
(SELECT CONCAT(LAT, LON) FROM Insurance GROUP BY LAT, LON HAVING COUNT(*) = 1)
This subquery groups the Insurance table by LAT and LON and returns those concatenated LAT and LON values which appear exactly once (HAVING COUNT(*) = 1).
Summary:
The query filters the records from the Insurance table where:

The tiv_2015 value appears in multiple rows.
The combination of LAT and LON values appears in exactly one row.
Finally, it computes the sum of the tiv_2016 values for these filtered records and returns this sum as tiv_2016.
Рекомендации по теме
welcome to shbcf.ru