汇总日内数据

时间:2014-01-08 13:41:59

标签: mysql netbeans-7

我有库存数据存储@ frame 1分钟,所以1行= 1分钟,我使用此查询来获取每天的最小值,最大值,总和,第一个和最后一个记录。

我每天有511分钟的行(从0900到1730)。

SELECT  MAX(high), MIN(low), SUM(volume) from a2a where date = 20131202; 
SELECT open FROM a2a ORDER BY time LIMIT 1;
SELECT close FROM a2a  WHERE time = 1730 ORDER BY time DESC LIMIT 1; 

SELECT  MAX(high), MIN(low), SUM(volume) from a2a where date = 20131203; 
SELECT open FROM a2a ORDER BY time LIMIT 1;
SELECT close FROM a2a  WHERE time = 1730 ORDER BY time DESC LIMIT 1;

SELECT  MAX(high), MIN(low), SUM(volume) from a2a where date = 20131204; 
SELECT open FROM a2a ORDER BY time LIMIT 1;
SELECT close FROM a2a  WHERE time = 1730 ORDER BY time DESC LIMIT 1;

在Netbeans中,这会返回9个标签:

我的问题是:

1)有没有办法在单个标签中获得这些结果?

2)我是否需要每天执行3次不同的选择,或者是否有办法在单个查询中完成该操作?

3)有没有比这更快的方法来计算这些聚合?发布的内容就是示例,我需要获取数百个每日数据。

编辑:表a2a图片。日期和时间类型为CHAR,ope为关闭为DOUBLE且volume为INTEGER。

enter image description here

2 个答案:

答案 0 :(得分:2)

尝试使用高/低/音量

 SELECT symbol,
        date,
        MAX(high)    AS dailyhigh,
        MIN(low)     AS dailylow,
        SUM(volume)  AS dailyvolume
  GROUP BY symbol, date

每个符号每天会获得一行。

很难说你如何处理开盘价。当然,即使没有日期,整个表格中的第一个时间戳也不是正确的方法。所以,让我们计算出每个日期的最早时间戳。

 SELECT symbol,
        date,
        MIN(time) AS opentime
   FROM a2a
  GROUP BY symbol, date

现在,我们需要查看该时间戳的开盘价。怎么做?它变得有点复杂,因为对于该问题的特定时间和日期,您可能有多行。

 SELECT a.symbol, 
        a.date,
        AVG (a.open) AS dailyopen
   FROM a2a a
       JOIN (
         SELECT symbol,
                date,
                MIN(time) AS opentime
           FROM a2a
          GROUP BY symbol, date 
        ) AS b   ON a.symbol = b.symbol 
                AND a.date = b.date
                AND a.time = b.opentime
  GROUP BY a.symbol, a.date

这会拉出每个符号和日期的开场记录(最低时间记录)。

现在,在我看来,你的结账记录是时间戳为1730的记录,或时间戳小于1730的最新记录。这是从snappy SQL转变为实际工作,但这些是财务记录和麦道夫在监狱里,让我们做对了。这是每个日期每个符号的结束时间。

 SELECT symbol,
        date,
        MAX(time) AS closetime
   FROM a2a
  WHERE time <= 1730
  GROUP BY symbol, date

现在我们需要每个日期的每个符号的收盘价。我们将以与检索开盘价相同的方式检索。

 SELECT a.symbol, 
        a.date,
        AVG (a.close) AS dailyclose
   FROM a2a a
       JOIN (
         SELECT symbol,
                date,
                MAX(time) AS closetime
           FROM a2a
          WHERE time <= 1730
          GROUP BY symbol, date
        ) AS b   ON a.symbol = b.symbol
                AND a.date = b.date
                AND a.time = b.closetime
  GROUP BY a.symbol, a.date

下一步是将这三个摘要查询加在一起,并限制您正在处理的日期范围。

SELECT d.symbol, d.date, d.dailyhigh, d.dailylow, d.dailyvolume,
       o.dailyopen,
       c.dailyclose
  FROM (
      /* the first summary query, daily high low volume */
       ) AS d
  JOIN (
      /* the second summary query, daily open */ 
       ) AS o ON d.symbol = o.symbol AND d.date = o.date
  JOIN (
      /* the third summary query, daily close */ 
       ) AS c ON d.symbol = c.symbol AND d.date = c.date
 WHERE d.date >= DATE_FORMAT(NOW() - INTERVAL 3 DAY, '%Y%m%d') 
   AND d.date  < DATE_FORMAT(NOW(), '%Y%m%d')
 ORDER BY d.symbol, d.date

这将为您提供三天的数据,以昨天结束。您必须将三个摘要查询剪切并粘贴到这个大胖连接中。我有点希望你能这样做,因为格式化深层嵌套的查询并不是很有趣。

这并不简单,但它对您的业务规则非常强大和明确。

答案 1 :(得分:1)

您可以使用相关子查询来打开和关闭:

  SELECT symbol, date, MAX(high) AS high, MIN(low) as low, SUM(volume) as volume,
         (select open from a2a a2 where a1.symbol = a2.symbol and a1.date = a2.date order by time limit 1) as open,
         (select close from a2a a2 where a1.symbol = a2.symbol and a1.date = a2.date order by time desc limit 1) as close
  FROM a2a a1
  GROUP BY symbol, date
  ORDER BY symbol, date;

a2a(symbol, date, time)上的索引会加快此查询速度。