【LeetCode】1070. 产品销售分析 III

销售表 Sales

+-------------+-------+
| Column Name | Type  |
+-------------+-------+
| sale_id     | int   |
| product_id  | int   |
| year        | int   |
| quantity    | int   |
| price       | int   |
+-------------+-------+
(sale_id, year) 是这张表的主键(具有唯一值的列的组合)。
product_id 是产品表的外键(reference 列)。
这张表的每一行都表示:编号 product_id 的产品在某一年的销售额。
请注意,价格是按每单位计的。

产品表 Product

+--------------+---------+
| Column Name  | Type    |
+--------------+---------+
| product_id   | int     |
| product_name | varchar |
+--------------+---------+
product_id 是这张表的主键(具有唯一值的列)。
这张表的每一行都标识:每个产品的 id 和 产品名称。

编写解决方案,选出每个售出过的产品 第一年 销售的 产品 id年份数量 和 价格

结果表中的条目可以按 任意顺序 排列。

结果格式如下例所示:

示例 1:

输入:
Sales 表:
+---------+------------+------+----------+-------+
| sale_id | product_id | year | quantity | price |
+---------+------------+------+----------+-------+ 
| 1       | 100        | 2008 | 10       | 5000  |
| 2       | 100        | 2009 | 12       | 5000  |
| 7       | 200        | 2011 | 15       | 9000  |
+---------+------------+------+----------+-------+
Product 表:
+------------+--------------+
| product_id | product_name |
+------------+--------------+
| 100        | Nokia        |
| 200        | Apple        |
| 300        | Samsung      |
+------------+--------------+
输出:
+------------+------------+----------+-------+
| product_id | first_year | quantity | price |
+------------+------------+----------+-------+ 
| 100        | 2008       | 10       | 5000  |
| 200        | 2011       | 15       | 9000  |
+------------+------------+----------+-------+

方法一:使用窗口函数筛选第一年

select
product_id,year as first_year,quantity,price
from
    (select
    product_id,quantity,price,year,
    dense_rank() over(partition by product_id order by year) as rnk
    from
    Sales)tmp
where rnk=1

方法二:使用group by聚合,group by要跟聚合函数一起使用——min()

select
product_id,year as first_year,quantity,price
from
Sales
where (product_id,year)
in
(select product_id,min(year) from Sales group by product_id)

相关推荐

  1. LeetCode1070. 产品销售分析 III

    2024-01-07 00:30:04       61 阅读
  2. LeetCode # 1070. 产品销售分析 III

    2024-01-07 00:30:04       30 阅读
  3. 高频SQL 产品销售分析 I

    2024-01-07 00:30:04       42 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-07 00:30:04       91 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-07 00:30:04       97 阅读
  3. 在Django里面运行非项目文件

    2024-01-07 00:30:04       78 阅读
  4. Python语言-面向对象

    2024-01-07 00:30:04       88 阅读

热门阅读

  1. Qt3D类使用说明

    2024-01-07 00:30:04       53 阅读
  2. ros python 接收GPS RTK 串口消息再转发 ros 主题消息

    2024-01-07 00:30:04       63 阅读
  3. Ubuntu中安装和配置SSH的完全指南

    2024-01-07 00:30:04       54 阅读
  4. go 使用 sync.RWMutex

    2024-01-07 00:30:04       67 阅读
  5. ROS 传感器—相机的介绍

    2024-01-07 00:30:04       49 阅读
  6. 存储过程从表中获取数据库名称

    2024-01-07 00:30:04       47 阅读
  7. CAD二开—WblockCloneObjects函数用法

    2024-01-07 00:30:04       55 阅读
  8. 针对CSP-J/S的冲刺练习:Day 3 小结

    2024-01-07 00:30:04       57 阅读
  9. 【PostgreSQL】模式Schema

    2024-01-07 00:30:04       49 阅读
  10. C++11_右值引用

    2024-01-07 00:30:04       43 阅读
  11. 单链表的尾插

    2024-01-07 00:30:04       59 阅读
  12. Peter算法小课堂—树的应用

    2024-01-07 00:30:04       57 阅读
  13. C#(winform)button去掉边框

    2024-01-07 00:30:04       59 阅读