用 SQL 找出某只股票连续上涨的最长天数

涉及多张中间表:

sql 复制代码
SELECT MAX(consecutive_day)
FROM (SELECT COUNT(*) as consecutive_day
  FROM (SELECT trade_date, SUM(rise_mark) OVER (ORDER BY trade_date) AS days_no_gain
     FROM (SELECT trade_date,
                CASE
                    WHEN closing_price > LAG(closing_price) OVER (ORDER BY trade_date)
                         THEN 0
                    ELSE 1 END AS rise_mark
           FROM stack_price) subquery1) subquery2
  GROUP BY days_no_gain) subquery3;

Over 语法

sql 复制代码
SELECT
  product_id,
  sale_date,
  sale_amount,
  SUM(sale_amount) OVER (PARTITION BY product_id ORDER BY sale_date) AS total_sales,
  SUM(sale_amount) OVER (PARTITION BY product_id) AS running_total
FROM
  sales;

basic:

sql 复制代码
order_id | customer_id | order_amount
-------------------------------------
1        | 1           | 100
2        | 1           | 150
3        | 2           | 200
4        | 2           | 50
5        | 2           | 120

result:

sql 复制代码
order_id | customer_id | order_amount | total_amount | running_total
-------------------------------------------------------------------
1        | 1           | 100          | 100          | 250
2        | 1           | 150          | 250          | 250
3        | 2           | 200          | 200          | 370
4        | 2           | 50           | 250          | 370
5        | 2           | 120          | 370          | 370

Window function

A window function is a type of function in SQL that performs calculations across a set of rows called a "window." The window is defined by the OVER clause, which specifies the partitioning and ordering of the rows.

SUM(order_amount) OVER (PARTITION BY customer_id ORDER BY order_id):

SUM(order_amount): This is the window function itself, in this case, the SUM function is used to calculate the sum of the order_amount.

OVER: It introduces the window function and specifies the window's characteristics.

PARTITION BY customer_id: This clause divides the rows into separate partitions based on the customer_id. Each partition will have its own calculation of the sum.

ORDER BY order_id: This clause determines the order in which the rows are processed within each partition. In this case, it orders the rows by the order_id.

SUM(order_amount) OVER (PARTITION BY customer_id):

This is another usage of the SUM window function, but without specifying the ordering using ORDER BY. Without the ORDER BY clause, the entire partition is considered, and the calculation is performed on all rows with the same customer_id.

The window function, in combination with the OVER clause, allows us to perform calculations within specific partitions and orderings defined by the columns specified. It provides a way to aggregate or calculate values based on a subset of rows without collapsing the result set or using subqueries.

Other common window functions include ROW_NUMBER(), AVG(), MIN(), MAX(), and LEAD()/LAG(), among others. Each function has its own specific purpose and behavior within the window frame defined by the OVER clause.

OLAP / OLTP

SQL 作为查询语言而发明, 名字叫 "结构化查询"(structured query), 数学基础是 "关系模型", 没有考虑复杂计算 (与之相对的是离散数学, 把 "数据存储 + 数据计算" 做在一起)

近年来, 数据处理和计算的需求越来越大, 于是 OLAP(联机分析处理)和 OLTP(联机事务处理)的概念就诞生了.

  • OLAP: Online Analytical Processing.
  • OLTP: Online Transaction Processing.

它们基于数据库, 属于"数据库 + 计算层".

处理海量数据, 有效率瓶颈.

相关推荐
瀚高PG实验室9 分钟前
HGDB V9.0.5 .pgpass 免密登录提示 IsEncryptNon-base64 encoding 警告处理
数据库·postgresql·瀚高数据库
龙腾AI白云26 分钟前
大模型检索增强生成(RAG)的技术原理与架构设计
数据库·python·机器学习·知识图谱
KaiwuDB37 分钟前
KaiwuDB 运维实战06:数据库备份与恢复:从理论到实战
运维·数据库·时序数据库·kaiwudb
数字新视界1 小时前
空调控制器AIRC800的智能化管理与应用实践
大数据·服务器·数据库·动环监控系统·动力与环境监控系统
学IT的周星星2 小时前
LangGraph:用「流程图」的方式编排 Agent
数据库·人工智能·大模型·llm·流程图·agent
吃一碗粉只付一碗钱2 小时前
10-令牌里的用户主键不要明文
服务器·数据库·sql·sqlserver·c#
蹄蹄2 小时前
从“一次性检测“到“年度服务“:检测机构收入结构变了,合同和应收账款也要跟着变
大数据·数据库·人工智能
福大大架构师每日一题2 小时前
redis 8.10.3正式发布:聚焦集群总线超大消息传播场景的安全修复,8.10.3 Latest更新解读
数据库·redis·安全
TDengine (老段)3 小时前
TDengine IDMP 十大实用功能(3)
大数据·数据库·物联网·时序数据库·iot·tdengine·涛思数据