LeetCode MySQL 1511. Customer Order Frequency

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

Table: Customers

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| name          | varchar |
| country       | varchar |
+---------------+---------+
customer_id is the primary key for this table.
This table contains information of the customers in the company.

Table: Product

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| product_id    | int     |
| description   | varchar |
| price         | int     |
+---------------+---------+
product_id is the primary key for this table.
This table contains information of the products in the company.
price is the product cost.

Table: Orders

+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| order_id      | int     |
| customer_id   | int     |
| product_id    | int     |
| order_date    | date    |
| quantity      | int     |
+---------------+---------+
order_id is the primary key for this table.
This table contains information on customer orders.
customer_id is the id of the customer who bought "quantity" products with id "product_id".
Order_date is the date in format ('YYYY-MM-DD') when the order was shipped.

Write an SQL query to report the customer_id and customer_name of customers who have spent at least $100 in each month of June and July 2020.

Return the result table in any order.

The query result format is in the following example.

Customers
+--------------+-----------+-------------+
| customer_id  | name      | country     |
+--------------+-----------+-------------+
| 1            | Winston   | USA         |
| 2            | Jonathan  | Peru        |
| 3            | Moustafa  | Egypt       |
+--------------+-----------+-------------+Product
+--------------+-------------+-------------+
| product_id   | description | price       |
+--------------+-------------+-------------+
| 10           | LC Phone    | 300         |
| 20           | LC T-Shirt  | 10          |
| 30           | LC Book     | 45          |
| 40           | LC Keychain | 2           |
+--------------+-------------+-------------+Orders
+--------------+-------------+-------------+-------------+-----------+
| order_id     | customer_id | product_id  | order_date  | quantity  |
+--------------+-------------+-------------+-------------+-----------+
| 1            | 1           | 10          | 2020-06-10  | 1         |
| 2            | 1           | 20          | 2020-07-01  | 1         |
| 3            | 1           | 30          | 2020-07-08  | 2         |
| 4            | 2           | 10          | 2020-06-15  | 2         |
| 5            | 2           | 40          | 2020-07-01  | 10        |
| 6            | 3           | 20          | 2020-06-24  | 2         |
| 7            | 3           | 30          | 2020-06-25  | 2         |
| 9            | 3           | 30          | 2020-05-08  | 3         |
+--------------+-------------+-------------+-------------+-----------+Result table:
+--------------+------------+
| customer_id  | name       |  
+--------------+------------+
| 1            | Winston    |
+--------------+------------+ 
Winston spent $300 (300 * 1) in June and $100 ( 10 * 1 + 45 * 2) in July 2020.
Jonathan spent $600 (300 * 2) in June and $20 ( 2 * 10) in July 2020.
Moustafa spent $110 (10 * 2 + 45 * 2) in June and $0 in July 2020.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/customer-order-frequency
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

  • 返回6月和7月都花了至少100美元的人
# Write your MySQL query statement below
select o.customer_id, c.name
from Customers c, Product p, Orders o
where c.customer_id = o.customer_id and p.product_id = o.product_id
group by o.customer_id
having 
(sum(case when o.order_date like '2020-06%' then o.quantity*p.price else 0 end) >= 100andsum(case when o.order_date like '2020-07%' then o.quantity*p.price else 0 end) >= 100
)

762 ms


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/474958.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode MySQL 1075. 项目员工 I

文章目录1. 题目2. 解题1. 题目 项目表 Project: ---------------------- | Column Name | Type | ---------------------- | project_id | int | | employee_id | int | ---------------------- 主键为 (project_id, employee_id)。 employee_id 是员…

The Shapes of CSS

http://home.cnblogs.com/blog/转载于:https://www.cnblogs.com/yanyanhappy/archive/2012/09/07/2675050.html

数据分析-书籍整理(一)

入门书籍 《谁说菜鸟不会数据分析》讲解了一些常见的分析技巧,并附带 Excel 的一些知识以及职场可使用的基础数据分析。 《MYSQL必知必会》对于有sql基础的人来说,可以快速复习知识点,小白也能学习到很多,不会打击自信心。我是用…

hdu 1054 Strategic Game 最小点覆盖 = 最大二分匹配

题目地址&#xff1a;http://acm.hdu.edu.cn/showproblem.php?pid1054 简单二分匹配&#xff0c;根据题意构造一个无向图。然后求最小点覆盖&#xff0c;然后扫描mark数组将曾经匹配的点所匹配的边消去。 最小点覆盖 最大二分匹配 #include<stdio.h> #include<strin…

数据分析统计学基础一

文章目录数据分析方法分类1、单纯的数据加工方法2、基于数理统计的数据分析方法3、基于数据挖掘的数据分析方法4、基于大数据的数据分析方法描述性统计分析1、数据的计量尺度2、数据的集中趋势3、数据的离中趋势4、数据分布的测定正态分布a. 偏态系数b. 数据峰度c. 偏度与峰度的…

LeetCode MySQL 1435. 制作会话柱状图

文章目录1. 题目2. 解题1. 题目 表&#xff1a;Sessions ------------------------------ | Column Name | Type | ------------------------------ | session_id | int | | duration | int | ------------------------------ sessio…

SharePoint2010网站备份还原简单介绍

今天尝试了SharePoint2010网站的备份和还原&#xff0c;从网上搜一些文档看看&#xff0c;然后自己试试&#xff0c;感觉和2007的操作没什么太大的区别&#xff0c;跟大家分享下自己尝试的命令行方式和数据库的方式&#xff0c;2007自己常用的也是这两种方式。 1、 命令行的方式…

LeetCode MySQL 1211. 查询结果的质量和占比

文章目录1. 题目2. 解题1. 题目 查询表 Queries&#xff1a; ---------------------- | Column Name | Type | ---------------------- | query_name | varchar | | result | varchar | | position | int | | rating | int | ---------------------…

数据分析方法-聚类算法

文章目录一、定义二、聚类、分类区别分类三、聚类常用算法1.划分聚类 k-means、k-medoids、k-modes、k-medians、kernel k-means2.层次聚类 Agglomerative 、divisive、BIRCH、ROCK、Chameleon3.密度聚类 DBSCAN、OPTICS5.模型聚类 GMM6.图聚类 Spectral Clustering&#xff08…

Java6 WebService学习

参考了网络上中资料&#xff0c;自己学习实践了一个Java6 WebService. 首先&#xff0c;建立一个WebService: package garfield;import javax.jws.WebService;import javax.xml.ws.Endpoint;WebServicepublic class MyJ6WebService {public String SayHello(String strName) {r…

公司人员离职情况分析及预测(工具:python)

前言 目前社会上呈现出一种公司招不到人&#xff0c;大批失业人员的矛盾现象&#xff0c;且大部分公司的离职率居高不下&#xff0c;很多入职没多久就辞职&#xff0c;所花费的培训招聘等资源都浪费了。为了弄清楚公司员工离职原因&#xff0c;通过kaggle上某一家企业员工离职…

LeetCode MySQL 1241. 每个帖子的评论数

文章目录1. 题目2. 解题1. 题目 表 Submissions 结构如下&#xff1a; ------------------------- | 列名 | 类型 | ------------------------- | sub_id | int | | parent_id | int | ------------------------- 上表没有主键, 所以可能…

MYSQL快速导入大量数据

创建数据表&#xff0c;并导入 1.根据数据特征建表&#xff0c;create语句 2.语句导入数据 LOAD DATA INFILE D:/UsersBehavior.csv INTO TABLE users CHARACTER SET utf8 FIELDS TERMINATED BY , OPTIONALLY ENCLOSED BY " ESCAPED BY " LINES TERMINATE…

新浪微博数据网络舆情分析客户端软件

这是我们北邮某实验室完全自主开发的数据挖掘和分析软件&#xff0c;用于处理海量数据&#xff0c;建立从数据采集&#xff0c;整理&#xff0c;分析&#xff0c;可视化&#xff0c;存储的全部服务。目前程序正在不断开发中。将在后期发布其测试版本。 转载于:https://www.cnbl…

LeetCode MySQL 1141. 查询近30天活跃用户数

文章目录1. 题目2. 解题1. 题目 活动记录表&#xff1a;Activity ------------------------ | Column Name | Type | ------------------------ | user_id | int | | session_id | int | | activity_date | date | | activity_type | enum | ---…

MYSQL错误: ERROR 1205: Lock wait timeout exceeded(处理MYSQL锁等待)解决办法

在运行数据库某一语句的时候&#xff08;数据量大概有一亿条&#xff09;&#xff0c;运行的特别慢&#xff0c;可能是我自己电脑配置不高&#xff0c;因此想删选一些数据但是运行的时候报1205错误。 原因 有会话执行过DML操作&#xff0c;然后没commit提交&#xff0c;再执行…

(jquery插件)打造百分比动态色彩条

以前没写过jquery插件&#xff0c;在开发这个时&#xff0c;写一下代码&#xff0c;又看一下jquery插件的定义&#xff08;如何开发之类的东东&#xff09;&#xff0c;来来去去的。 之所以要写这个插件&#xff0c;主要是为了往后的项目中方便实现这类型的功能&#xff0c;在之…

LeetCode MySQL 1517. Find Users With Valid E-Mails(正则表达式)

文章目录1. 题目2. 解题1. 题目 Table: Users ------------------------ | Column Name | Type | ------------------------ | user_id | int | | name | varchar | | mail | varchar | ------------------------user_id is the primary ke…

数据分析-书籍整理(三)

《淘宝网店大数据营销》一套网店搭建的整体体系&#xff0c;从如何做好网店展开说&#xff0c;有很多实用方法&#xff0c;对运营有很大的帮助&#xff0c;方便数据分析人员理解业务 《京东平台数据化运营》讲解了一些京东的知识&#xff0c;很多更淘宝运营相似&#xff0c;只…

基于消息队列的日志组件

1 简介 1.1 类图 1.2 说明 日志支持的存储方式有&#xff1a;文本&#xff0c;xml,数据库三种。如果采用数据库方式&#xff0c;支持的数据库有sql server、oracle。日志采用读写分离的方式&#xff0c;客户端发送日志到消息队列&#xff0c;然后服务端程序读取消息队列&…