我是 sql 查询的新手。表定义为
( symbol varchar,
high int,
low int,
today date,
Primary key (symbol, today)
)
我需要找到给定日期范围内的每个符号,max(high)
以及和min(low)
的相应日期。max(high)
min(low)
- 可以在给定表中获取第一个最大日期和最小日期。
- 在给定的日期范围内,某些日期可能会丢失。如果开始日期不存在,则应使用下一个日期,如果最后日期不存在,则应使用较早的可用日期
数据为一年和大约 5000 个符号。
我尝试过这样的事情
SELECT a.symbol,
a.maxValue,
a.maxdate,
b.minValue,
b.mindate
FROM (
SELECT table1.symbol, max_a.maxValue, max_a.maxdate
FROM table1
INNER JOIN (
SELECT table1.symbol,
max(table1.high) AS maxValue,
table1.TODAY AS maxdate
FROM table1
GROUP BY table1.symbol
) AS max_a
ON max_a.symbol = table1.symbol
AND table1.today = max_a.maxdate
) AS a
INNER JOIN (
SELECT symbol,
min_b.minValue,
min_b.mindate
FROM table1
INNER JOIN (
SELECT symbol,
min(low) AS minValue,
table1.TODAY AS mindate
FROM table1
GROUP BY testnsebav.symbol
) AS min_b
ON min_b.symbol = table1.symbol
AND table1.today = min_b.mindate
) AS b
ON a.symbol = b.symbol