暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

单挑力扣(LeetCode)SQL笔试题:1421. 净现值查询(难度:简单)

跟强哥学SQL 2023-03-23
99
题目:1421. 净现值查询
(通过次数5,757 | 提交次数8,107,通过率71.01%)
    表: NPV
    +---------------+---------+
    | Column Name | Type |
    +---------------+---------+
    | id | int |
    | year | int |
    | npv | int |
    +---------------+---------+
    (id, year) 是该表主键.
    该表有每一笔存货的年份, id 和对应净现值的信息.
    表: Queries
    +---------------+---------+
    | Column Name | Type |
    +---------------+---------+
    | id | int |
    | year | int |
    +---------------+---------+
    (id, year) 是该表主键.
    该表有每一次查询所对应存货的 id 和年份的信息.


    写一个 SQL,找到 Queries表中每一次查询的净现值.
    结果表没有顺序要求.
    查询结果的格式如下所示:
    NPV 表:
    +------+--------+--------+
    | 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 表:
    +------+--------+
    | id | year |
    +------+--------+
    | 1 | 2019 |
    | 2 | 2008 |
    | 3 | 2009 |
    | 7 | 2018 |
    | 7 | 2019 |
    | 7 | 2020 |
    | 13 | 2019 |
    +------+--------+
    结果表:
    +------+--------+--------+
    | id | year | npv |
    +------+--------+--------+
    | 1 | 2019 | 113 |
    | 2 | 2008 | 121 |
    | 3 | 2009 | 12 |
    | 7 | 2018 | 0 |
    | 7 | 2019 | 0 |
    | 7 | 2020 | 30 |
    | 13 | 2019 | 40 |
    +------+--------+--------+
    (7, 2018)的净现值不在 NPV 表中, 我们把它看作是 0.
    所有其它查询的净现值都能在 NPV 表中找到.


    来源:力扣(LeetCode)
    链接:https://leetcode.cn/problems/npv-queries
    复制

      #测试数据
      Create Table If Not Exists NPV (id int, year int, npv int);
      Create Table If Not Exists Queries (id int, year int);


      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');


      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');
      复制
      解题思路:
      NPV表保存了所有年份的存货。
      Queries表保存了所有的查询请求。
      题目要求:返回所有查询请求对应的存货。
      不存在的查询请求也需要返回,很明显,这是一个考查左关联的题目。当查询请求在NPV表不存在时,需要使用空值处理函数COALESCE返回0。
      参考SQL:
        select
        a.id,
        a.year,
        coalesce(b.npv,0) npv
        from Queries a
        left join NPV b
        on a.id = b.id
        and a.year = b.year;
        复制

        来与ChatGPT比比谁的编程能力强?
        单挑力扣(LeetCode)SQL笔试题:1350. 院系无效的学生(难度:简单)
        单挑力扣(LeetCode)SQL笔试题:1378. 使用唯一标识码替换员工ID(难度:简单)


        笔试题合集免费领取方法


        方法一:关注公众号【跟强哥学SQL】,回复关键字【力扣】获取链接。

        方法二:访问【SQL网】:https://sql.wang/sql-leetcode/sql-exercise



        文章转载自跟强哥学SQL,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

        评论