【SQL】1327. 列出指定时间段内所有的下单产品

前述

知识点:sql中用JOIN USING 简化JOIN ON

题目描述

leetcode 题目:1327. 列出指定时间段内所有的下单产品



Code

写法一

先过滤,再连表

sql 复制代码
-- 写法一:先过滤再连表
select B.product_name, A.summ as unit
from(
    select product_id,
        sum(unit) as summ
    from Orders
    where order_date between '2020-02-01' and '2020-02-29'
    group by product_id
    having summ >= 100
) A
left join Products B
on A.product_id = B.product_id;

写法二

先连表,再过滤

sql 复制代码
-- 写法二:先连表再过滤
select product_name, sum(unit) as unit
from Products P
left join Orders O
using(product_id)
-- where order_date between '2020-02-01' and '2020-02-29'
-- where order_date >= '2020-02-01' and order_date <='2020-02-29'
-- where year(order_date) = 2020 and month(order_date) = 02
where order_date like '2020-02%'
group by O.product_id
having sum(unit) >= 100;

不同的过滤条件写法,都行。

相关推荐
寒月小酒5 小时前
第五章 生成集成 和第六章 RAG评估(all-in-rag学习)
数据库·学习
AI人工智能集结号5 小时前
AI回答采集:基于PostgreSQL JSONB的原始数据存储与可追溯性方案
数据库·人工智能·postgresql
躺柒6 小时前
读半导体简史08大型机
linux·服务器·c语言·数据库·unix·ibm
DFT计算杂谈6 小时前
交错磁研究进展材料物性与交叉应用
数据库·人工智能·python·opencv·算法
玖玥拾6 小时前
LeetCode 26 删除有序数组中的重复项
算法·leetcode
IvorySQL6 小时前
PG 日报|查询优化器重大修复,支持更多复杂 SQL 优化
数据库·人工智能·postgresql·开源
写代码的强哥7 小时前
TiDB 和 OceanBase 对比:架构师视角下的企业选型实战指南
数据库·云原生·架构
白白白小纯8 小时前
算法篇—返回倒数第k个节点
c语言·数据结构·算法·leetcode
金融小白数据分析之路8 小时前
绍兴市镇街echarts 制作
前端·数据库·echarts
桐薇全肯定9 小时前
MySQL学生成绩管理系统实战操作
java·数据库·sql