-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1421-NPVQueries.sql
More file actions
99 lines (95 loc) · 3.33 KB
/
1421-NPVQueries.sql
File metadata and controls
99 lines (95 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
-- 1421. NPV Queries
-- Table: NPV
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | id | int |
-- | year | int |
-- | npv | int |
-- +---------------+---------+
-- (id, year) is the primary key (combination of columns with unique values) of this table.
-- The table has information about the id and the year of each inventory and the corresponding net present value.
-- Table: Queries
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | id | int |
-- | year | int |
-- +---------------+---------+
-- (id, year) is the primary key (combination of columns with unique values) of this table.
-- The table has information about the id and the year of each inventory query.
-- Write a solution to find the npv of each query of the Queries table.
-- Return the result table in any order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- NPV table:
-- +------+--------+--------+
-- | id | year | npv |
-- +------+--------+--------+
-- | 1 | 2018 | 100 |
-- | 7 | 2020 | 30 |
-- | 13 | 2019 | 40 |
-- | 1 | 2019 | 113 |
-- | 2 | 2008 | 121 |
-- | 3 | 2009 | 12 |
-- | 11 | 2020 | 99 |
-- | 7 | 2019 | 0 |
-- +------+--------+--------+
-- Queries table:
-- +------+--------+
-- | id | year |
-- +------+--------+
-- | 1 | 2019 |
-- | 2 | 2008 |
-- | 3 | 2009 |
-- | 7 | 2018 |
-- | 7 | 2019 |
-- | 7 | 2020 |
-- | 13 | 2019 |
-- +------+--------+
-- Output:
-- +------+--------+--------+
-- | id | year | npv |
-- +------+--------+--------+
-- | 1 | 2019 | 113 |
-- | 2 | 2008 | 121 |
-- | 3 | 2009 | 12 |
-- | 7 | 2018 | 0 |
-- | 7 | 2019 | 0 |
-- | 7 | 2020 | 30 |
-- | 13 | 2019 | 40 |
-- +------+--------+--------+
-- Explanation:
-- The npv value of (7, 2018) is not present in the NPV table, we consider it 0.
-- The npv values of all other queries can be found in the NPV table.
-- Create Table If Not Exists NPV (id int, year int, npv int)
-- Create Table If Not Exists Queries (id int, year int)
-- Truncate table NPV
-- insert into NPV (id, year, npv) values ('1', '2018', '100')
-- insert into NPV (id, year, npv) values ('7', '2020', '30')
-- insert into NPV (id, year, npv) values ('13', '2019', '40')
-- insert into NPV (id, year, npv) values ('1', '2019', '113')
-- insert into NPV (id, year, npv) values ('2', '2008', '121')
-- insert into NPV (id, year, npv) values ('3', '2009', '21')
-- insert into NPV (id, year, npv) values ('11', '2020', '99')
-- insert into NPV (id, year, npv) values ('7', '2019', '0')
-- Truncate table Queries
-- insert into Queries (id, year) values ('1', '2019')
-- insert into Queries (id, year) values ('2', '2008')
-- insert into Queries (id, year) values ('3', '2009')
-- insert into Queries (id, year) values ('7', '2018')
-- insert into Queries (id, year) values ('7', '2019')
-- insert into Queries (id, year) values ('7', '2020')
-- insert into Queries (id, year) values ('13', '2019')
SELECT
q.id,
q.year,
IFNULL(n.npv,0) AS npv -- 净现值不在 NPV 表中, 我们把它看作是 0
FROM
Queries AS q
LEFT JOIN
NPV AS n
ON
n.id = q.id AND
n.year = q.year