You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
-- Write a query to print the sum of all total investment values in 2016 (TIV_2016), to a scale of 2 decimal places, for all policy holders who meet the following criteria:
3
+
4
+
-- Have the same TIV_2015 value as one or more other policyholders.
5
+
-- Are not located in the same city as any other policyholder (i.e.: the (latitude, longitude) attribute pairs must be unique).
6
+
-- Input Format:
7
+
-- The insurance table is described as follows:
8
+
9
+
-- | Column Name | Type |
10
+
-- |-------------|---------------|
11
+
-- | PID | INTEGER(11) |
12
+
-- | TIV_2015 | NUMERIC(15,2) |
13
+
-- | TIV_2016 | NUMERIC(15,2) |
14
+
-- | LAT | NUMERIC(5,2) |
15
+
-- | LON | NUMERIC(5,2) |
16
+
-- where PID is the policyholder's policy ID, TIV_2015 is the total investment value in 2015, TIV_2016 is the total investment value in 2016, LAT is the latitude of the policy holder's city, and LON is the longitude of the policy holder's city.
17
+
18
+
-- Sample Input
19
+
20
+
-- | PID | TIV_2015 | TIV_2016 | LAT | LON |
21
+
-- |-----|----------|----------|-----|-----|
22
+
-- | 1 | 10 | 5 | 10 | 10 |
23
+
-- | 2 | 20 | 20 | 20 | 20 |
24
+
-- | 3 | 10 | 30 | 20 | 20 |
25
+
-- | 4 | 10 | 40 | 40 | 40 |
26
+
-- Sample Output
27
+
28
+
-- | TIV_2016 |
29
+
-- |----------|
30
+
-- | 45.00 |
31
+
-- Explanation
32
+
33
+
-- The first record in the table, like the last record, meets both of the two criteria.
34
+
-- The TIV_2015 value '10' is as the same as the third and forth record, and its location unique.
35
+
36
+
-- The second record does not meet any of the two criteria. Its TIV_2015 is not like any other policyholders.
37
+
38
+
-- And its location is the same with the third record, which makes the third record fail, too.
39
+
40
+
-- So, the result is the sum of TIV_2016 of the first and last record, which is 45.
41
+
42
+
-- Solution
43
+
selectsum(TIV_2016) TIV_2016
44
+
from
45
+
(select*, count(*) over (partition by TIV_2015) as c1, count(*) over (partition by LAT, LON) as c2
46
+
from insurance ) t
47
+
where c1 >1and c2 =1;
Collapse file: Medium/Number of Trusted Contacts.sql
-- Each row of this table contains the name and the email of a customer of an online shop.
13
+
14
+
15
+
-- Table: Contacts
16
+
17
+
-- +---------------+---------+
18
+
-- | Column Name | Type |
19
+
-- +---------------+---------+
20
+
-- | user_id | id |
21
+
-- | contact_name | varchar |
22
+
-- | contact_email | varchar |
23
+
-- +---------------+---------+
24
+
-- (user_id, contact_email) is the primary key for this table.
25
+
-- Each row of this table contains the name and email of one contact of customer with user_id.
26
+
-- This table contains information about people each customer trust. The contact may or may not exist in the Customers table.
27
+
28
+
29
+
30
+
-- Table: Invoices
31
+
32
+
-- +--------------+---------+
33
+
-- | Column Name | Type |
34
+
-- +--------------+---------+
35
+
-- | invoice_id | int |
36
+
-- | price | int |
37
+
-- | user_id | int |
38
+
-- +--------------+---------+
39
+
-- invoice_id is the primary key for this table.
40
+
-- Each row of this table indicates that user_id has an invoice with invoice_id and a price.
41
+
42
+
43
+
-- Write an SQL query to find the following for each invoice_id:
44
+
45
+
-- customer_name: The name of the customer the invoice is related to.
46
+
-- price: The price of the invoice.
47
+
-- contacts_cnt: The number of contacts related to the customer.
48
+
-- trusted_contacts_cnt: The number of contacts related to the customer and at the same time they are customers to the shop. (i.e His/Her email exists in the Customers table.)
49
+
-- Order the result table by invoice_id.
50
+
51
+
-- The query result format is in the following example:
0 commit comments