Skip to content

Commit 1060857

Browse files
authored
Add files via upload
1 parent 4fc8fb5 commit 1060857

File tree

2 files changed

+147
-0
lines changed

2 files changed

+147
-0
lines changed

Medium/Investments in 2016.sql

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
-- Question 96
2+
-- 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+
select sum(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 > 1 and c2 = 1;
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
-- Question 97
2+
-- Table: Customers
3+
4+
-- +---------------+---------+
5+
-- | Column Name | Type |
6+
-- +---------------+---------+
7+
-- | customer_id | int |
8+
-- | customer_name | varchar |
9+
-- | email | varchar |
10+
-- +---------------+---------+
11+
-- customer_id is the primary key for this table.
12+
-- 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:
52+
53+
-- Customers table:
54+
-- +-------------+---------------+--------------------+
55+
-- | customer_id | customer_name | email |
56+
-- +-------------+---------------+--------------------+
57+
-- | 1 | Alice | [email protected] |
58+
-- | 2 | Bob | [email protected] |
59+
-- | 13 | John | [email protected] |
60+
-- | 6 | Alex | [email protected] |
61+
-- +-------------+---------------+--------------------+
62+
-- Contacts table:
63+
-- +-------------+--------------+--------------------+
64+
-- | user_id | contact_name | contact_email |
65+
-- +-------------+--------------+--------------------+
66+
-- | 1 | Bob | [email protected] |
67+
-- | 1 | John | [email protected] |
68+
-- | 1 | Jal | [email protected] |
69+
-- | 2 | Omar | [email protected] |
70+
-- | 2 | Meir | [email protected] |
71+
-- | 6 | Alice | [email protected] |
72+
-- +-------------+--------------+--------------------+
73+
-- Invoices table:
74+
-- +------------+-------+---------+
75+
-- | invoice_id | price | user_id |
76+
-- +------------+-------+---------+
77+
-- | 77 | 100 | 1 |
78+
-- | 88 | 200 | 1 |
79+
-- | 99 | 300 | 2 |
80+
-- | 66 | 400 | 2 |
81+
-- | 55 | 500 | 13 |
82+
-- | 44 | 60 | 6 |
83+
-- +------------+-------+---------+
84+
-- Result table:
85+
-- +------------+---------------+-------+--------------+----------------------+
86+
-- | invoice_id | customer_name | price | contacts_cnt | trusted_contacts_cnt |
87+
-- +------------+---------------+-------+--------------+----------------------+
88+
-- | 44 | Alex | 60 | 1 | 1 |
89+
-- | 55 | John | 500 | 0 | 0 |
90+
-- | 66 | Bob | 400 | 2 | 0 |
91+
-- | 77 | Alice | 100 | 3 | 2 |
92+
-- | 88 | Alice | 200 | 3 | 2 |
93+
-- | 99 | Bob | 300 | 2 | 0 |
94+
-- +------------+---------------+-------+--------------+----------------------+
95+
-- Alice has three contacts, two of them are trusted contacts (Bob and John).
96+
-- Bob has two contacts, none of them is a trusted contact.
97+
-- Alex has one contact and it is a trusted contact (Alice).
98+
-- John doesn't have any contacts.
99+
100+
-- Solution

0 commit comments

Comments
 (0)