返回介绍

solution / 1400-1499 / 1421.NPV Queries / README_EN

发布于 2024-06-17 01:03:19 字数 2682 浏览 0 评论 0 收藏 0

1421. NPV Queries

中文文档

Description

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.

Solutions

Solution 1

# Write your MySQL query statement below
SELECT q.*, IFNULL(npv, 0) AS npv
FROM
  Queries AS q
  LEFT JOIN NPV AS n USING (id, year);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文