将时间范围分成2分钟的间隔。

huangapple go评论61阅读模式
英文:

Grouping into interval of 2 minutes within a time range

问题

这是您要翻译的内容:

我在将数据分组为2分钟间隔时遇到了一些问题。
以下是我的表格数据:

标签 开盘价 最高价 最低价 收盘价 时间
APPL 1940 1950 1935 1948 2023-03-20 11:00:00
APPL 1950 1960 1945 1958 2023-03-20 11:01:00
APPL 1960 1970 1955 1968 2023-03-20 11:02:00
APPL 1970 1980 1965 1978 2023-03-20 11:03:00
APPL 1980 1990 1985 1998 2023-03-20 11:04:00
APPL 1990 2000 1975 1988 2023-03-20 11:05:00

我的查询:

select
  开盘价,
  MAX(最高价) as 最高价,
  MIN(最低价) as 最低价,
  收盘价,
  时间,
  FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(时间)/120 ) * 120) AS 15分钟区间
from 表名
where 标签 = 'APPL'
group by 15分钟区间
order by 15分钟区间 ASC

预期输出:

标签 开盘价 最高价 最低价 收盘价 时间
APPL 1940 1960 1935 1958 2023-03-20 11:00:00
APPL 1960 1980 1955 1978 2023-03-20 11:02:00
APPL 1980 2000 1975 1988 2023-03-20 11:04:00

您遇到的问题是只能获取范围内的最新收盘价。目前是从第一行而不是最后一行获取2分钟内的数据。

英文:

i have some issue with grouping the data in interval of 2 minutes.
here is my table data

ticker open high low close time
APPL 1940 1950 1935 1948 2023-03-20 11:00:00
APPL 1950 1960 1945 1958 2023-03-20 11:01:00
APPL 1960 1970 1955 1968 2023-03-20 11:02:00
APPL 1970 1980 1965 1978 2023-03-20 11:03:00
APPL 1980 1990 1985 1998 2023-03-20 11:04:00
APPL 1990 2000 1975 1988 2023-03-20 11:05:00

My query:

select
  open,
  MAX(high) as high,
  MIN(low) as low,
  close,
  time,
  FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(time)/120 ) * 120) AS quaterhour
from tableName
where ticker = 'APPL'
group by quaterhour
order by quaterhour ASC

Expected output:

ticker open high low close time
APPL 1940 1960 1935 1958 2023-03-20 11:00:00
APPL 1960 1980 1955 1978 2023-03-20 11:02:00
APPL 1980 2000 1975 1988 2023-03-20 11:04:00

I only have issues with getting the latest close price with in range. right now getting from first row not last row in 2 minute.

答案1

得分: 1

请尝试这个,我希望没有拼写错误:

select
  open,
  MAX(high) as high,
  MIN(low) as low,
  MAX(IF((minute(time) & 1),close,0)),
  time,
  time - INTERVAL (minute(time) & 1) MINUTE AS quarterhour
from tableName
where ticker = 'APPL'
group by quarterhour
order by quarterhour ASC;
英文:

Try this, i hope there is no typo

select
  open,
  MAX(high) as high,
  MIN(low) as low,
  MAX(IF((minute(time) & 1),close,0)),
  time,
  time - INTERVAL (minute(time) & 1) MINUTE AS quaterhour
from tableName
where ticker = 'APPL'
group by quaterhour
order by quaterhour ASC;

答案2

得分: 0

当禁用ONLY_FULL_GROUP_BY时,MySQL允许省略非聚合分组列,此时会返回一个随机行(通常是遇到的第一行)而不是聚合。您的查询就是这样。

列出所有非聚合列在分组中:

select
  open,
  MAX(high) as high,
  MIN(low) as low,
  close,
  FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(time)/120 ) * 120) AS quaterhour
from tableName
where ticker = 'APPL'
group by open, close, quaterhour
order by quaterhour ASC

您必须从选择中删除time,以便按时间进行聚合。

此外,“quarter”是一个不太合适的名称选择,用于表示2分钟的时间段。

英文:

When ONLY_FULL_GROUP_BY is disabled, MySQL allows non-aggregate grouping columns to be omitted, in which case it returns a random row (usually the first row encountered) instead of aggregating. Your query does this.

List all non-aggregating columns in the group by:

select
  open,
  MAX(high) as high,
  MIN(low) as low,
  close,
  FROM_UNIXTIME(FLOOR( UNIX_TIMESTAMP(time)/120 ) * 120) AS quaterhour
from tableName
where ticker = 'APPL'
group by open, close, quaterhour
order by quaterhour ASC

You must remove the time from the select to aggregate over time.

Also, "quarter" is a poor name choice for a 2-minute period.

huangapple
  • 本文由 发表于 2023年3月21日 01:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75793637-4.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定