mysql 查询以查找按时间变化分组的字段总和
mysql query to find sum of fields grouping by time change
我有如下数据结构
timestamp(varchar) bid(decimal) ask(decimal)
20090501 03:01:01.582 0.000060 0.000000
20090501 15:01:01.582 0.000120 0.000060
20090501 16:01:01.582 -0.000080 0.000120
20090504 03:01:01.582 0.000040 0.000060
20090504 15:01:01.582 -0.000040 0.000040
20090504 16:01:01.582 0.000000 -0.000040
20090505 03:01:01.582 0.000050 0.000110
20090505 15:01:01.582 0.000000 0.000050
20090505 16:01:01.582 -0.000080 0.000000
现在我想要如下输出
timestamp sum (bid) sum(ask)
20090501 15:01:01.582 0.000180 0.000060
20090504 15:01:01.582 -0.000080 0.000220
20090505 15:01:01.582 0.000050 0.000120
现在结果背后的关系逻辑是每次出现 15:01 时它都会对最后一次 15:01 出现的间隔内的所有出价和要价求和,这意味着出价和要价之间的总和每个15:01都需要计算
我正在尝试使用 MySQL,因此非常感谢对此的任何帮助。
到目前为止我完成的代码是在 Sql server 2008 R2
select date=case when substring(timestamp,10,2) <= 15
then substring(timestamp,1,8) else DATEADD("dd",1,substring(timestamp,1,8)) end,
SUM(isnull([Bid Change],0)), SUM([Ask Change]), MAX(aveg),MIN(aveg) from tbltestnew1
group by (case when substring(timestamp,10,2) <= 15
then substring(timestamp,1,8) else DATEADD("dd",1,substring(timestamp,1,8)) end),
CURR;
考虑到每个 15:01 的 1 天间隔,这给了我结果,这是不正确的结果!
select
case when time(timestamp) > '15:01:00'
THEN DATE(DATE_ADD(timestamp INTERVAL 1 DAY))
ELSE DATE(timestamp) END AS count_date,
SUM(isnull([Bid Change],0)),
SUM([Ask Change]),
MAX(aveg),
MIN(aveg) from tbltestnew1
group by count_date;
对于 MSSQL,您可以像这样使用 outer apply
:
select
cast(t.timestamp as date) date,
bid_sum,
ask_sum
from tbltestnew1 t
outer apply (
select top 1 timestamp tlag
from tbltestnew1
where t.timestamp > timestamp and cast(timestamp as time) = '15:01:01.582' order by timestamp desc
) tprev
outer apply (
select sum(bid) bid_sum, sum(ask) ask_sum
from tbltestnew1
where (tlag is not null and (cast(timestamp as datetime) between dateadd(second,1, tlag) and t.timestamp)
or (tlag is null and cast(timestamp as datetime) <= t.timestamp)
)
) x
where cast(t.timestamp as time) = '15:01:01.582';
这个查询会给出这个结果:
| DATE | BID_SUM | ASK_SUM |
|------------|----------|---------|
| 2009-05-01 | 0.00018 | 0.00006 |
| 2009-05-04 | -0.00008 | 0.00022 |
| 2009-05-05 | 0.00005 | 0.00012 |
在 MSSQL 2012+ 中,您可以使用 lag()
window 函数来访问之前的行(这是第一个外部应用所做的),它看起来像这样:
select cast(t.timestamp as date) date, sum_bid, sum_ask
from (select timestamp, ask, bid, lag(timestamp) over (order by timestamp) prev from tbltestnew1
where cast(timestamp as time) = '15:01:01.582') t
outer apply (
select sum(bid) sum_bid, sum(ask) sum_ask
from tbltestnew1
where (prev is not null and (cast(timestamp as datetime) between dateadd(second,1, prev) and t.timestamp)
or (prev is null and cast(timestamp as datetime) <= t.timestamp))
) oa
当然,您可以使用通用的 table 表达式(或派生的 tables)来减少转换次数。
根据您的示例数据,它似乎很简单 GROUP BY LEFT(timestamp, 8)
。
我有如下数据结构
timestamp(varchar) bid(decimal) ask(decimal)
20090501 03:01:01.582 0.000060 0.000000
20090501 15:01:01.582 0.000120 0.000060
20090501 16:01:01.582 -0.000080 0.000120
20090504 03:01:01.582 0.000040 0.000060
20090504 15:01:01.582 -0.000040 0.000040
20090504 16:01:01.582 0.000000 -0.000040
20090505 03:01:01.582 0.000050 0.000110
20090505 15:01:01.582 0.000000 0.000050
20090505 16:01:01.582 -0.000080 0.000000
现在我想要如下输出
timestamp sum (bid) sum(ask)
20090501 15:01:01.582 0.000180 0.000060
20090504 15:01:01.582 -0.000080 0.000220
20090505 15:01:01.582 0.000050 0.000120
现在结果背后的关系逻辑是每次出现 15:01 时它都会对最后一次 15:01 出现的间隔内的所有出价和要价求和,这意味着出价和要价之间的总和每个15:01都需要计算 我正在尝试使用 MySQL,因此非常感谢对此的任何帮助。
到目前为止我完成的代码是在 Sql server 2008 R2
select date=case when substring(timestamp,10,2) <= 15
then substring(timestamp,1,8) else DATEADD("dd",1,substring(timestamp,1,8)) end,
SUM(isnull([Bid Change],0)), SUM([Ask Change]), MAX(aveg),MIN(aveg) from tbltestnew1
group by (case when substring(timestamp,10,2) <= 15
then substring(timestamp,1,8) else DATEADD("dd",1,substring(timestamp,1,8)) end),
CURR;
考虑到每个 15:01 的 1 天间隔,这给了我结果,这是不正确的结果!
select
case when time(timestamp) > '15:01:00'
THEN DATE(DATE_ADD(timestamp INTERVAL 1 DAY))
ELSE DATE(timestamp) END AS count_date,
SUM(isnull([Bid Change],0)),
SUM([Ask Change]),
MAX(aveg),
MIN(aveg) from tbltestnew1
group by count_date;
对于 MSSQL,您可以像这样使用 outer apply
:
select
cast(t.timestamp as date) date,
bid_sum,
ask_sum
from tbltestnew1 t
outer apply (
select top 1 timestamp tlag
from tbltestnew1
where t.timestamp > timestamp and cast(timestamp as time) = '15:01:01.582' order by timestamp desc
) tprev
outer apply (
select sum(bid) bid_sum, sum(ask) ask_sum
from tbltestnew1
where (tlag is not null and (cast(timestamp as datetime) between dateadd(second,1, tlag) and t.timestamp)
or (tlag is null and cast(timestamp as datetime) <= t.timestamp)
)
) x
where cast(t.timestamp as time) = '15:01:01.582';
这个查询会给出这个结果:
| DATE | BID_SUM | ASK_SUM |
|------------|----------|---------|
| 2009-05-01 | 0.00018 | 0.00006 |
| 2009-05-04 | -0.00008 | 0.00022 |
| 2009-05-05 | 0.00005 | 0.00012 |
在 MSSQL 2012+ 中,您可以使用 lag()
window 函数来访问之前的行(这是第一个外部应用所做的),它看起来像这样:
select cast(t.timestamp as date) date, sum_bid, sum_ask
from (select timestamp, ask, bid, lag(timestamp) over (order by timestamp) prev from tbltestnew1
where cast(timestamp as time) = '15:01:01.582') t
outer apply (
select sum(bid) sum_bid, sum(ask) sum_ask
from tbltestnew1
where (prev is not null and (cast(timestamp as datetime) between dateadd(second,1, prev) and t.timestamp)
or (prev is null and cast(timestamp as datetime) <= t.timestamp))
) oa
当然,您可以使用通用的 table 表达式(或派生的 tables)来减少转换次数。
根据您的示例数据,它似乎很简单 GROUP BY LEFT(timestamp, 8)
。