mysql查询查找按时间变化分组的字段总和

时间:2015-03-02 14:46:38

标签: mysql sql-server-2008 mysql-workbench

我有以下数据结构

  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天间隔,这不是正确的结果!

3 个答案:

答案 0 :(得分:0)

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;

答案 1 :(得分:0)

使用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';

Sample SQL Fiddle

此查询会给出以下结果:

|       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()窗口函数来访问前面的行(这是第一个外部应用程序所做的),它看起来像这样:

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

当然,您可以通过使用公用表表达式(或派生表)来减少强制转换的数量。

答案 2 :(得分:0)

根据您的示例数据,它似乎就像GROUP BY LEFT(timestamp, 8)一样简单。