通过左连接获取错误并生成时间序列

时间:2014-12-19 10:40:44

标签: sql postgresql

我想通过获取错误来填充所有间隔

enter image description here

由第二个查询聚合的原文(可行)

    SELECT 
      date_trunc('second', ticktime) AS ticktime ,
      max(last_price) OVER w AS high ,
      min(last_price) OVER w AS low 
   FROM czces
   WHERE product_type ='TA' AND contract_month = '2014-08-01 00:00:00'::TIMESTAMP 
     WINDOW w AS (
      PARTITION BY date_trunc('second', ticktime)
      ORDER BY ticktime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
      )

错误:JOIN / USING类型bigint和无时区的时间戳无法匹配

SELECT 
       t.high,
       t.low
FROM 
(

  SELECT generate_series(
    date_trunc('second', min(ticktime)) ,
    date_trunc('second', max(ticktime)) ,
    interval '1 sec'
  ) FROM czces  AS g (time)

  LEFT JOIN
  (
    SELECT 
      date_trunc('second', ticktime) AS time ,
      max(last_price) OVER w AS high ,
      min(last_price) OVER w AS low 
   FROM czces
   WHERE product_type ='TA' AND contract_month = '2014-08-01 00:00:00'::TIMESTAMP 
     WINDOW w AS (
      PARTITION BY date_trunc('second', ticktime)
      ORDER BY ticktime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
      )
  ) t USING (time)



  ORDER BY 1 
) AS t ;

错误:FROM中的子查询必须具有别名LINE 5:(

    SELECT 
           t.high,
           t.low
    FROM 
    (

      SELECT generate_series(
        date_trunc('second', min(ticktime)) ,
        date_trunc('second', max(ticktime)) ,
        interval '1 sec'
      ) FROM  czces AS g(ticktime)

      LEFT JOIN
      (
        SELECT 
          date_trunc('second', ticktime) AS ticktime ,
          max(last_price) OVER w AS high ,
          min(last_price) OVER w AS low 
       FROM czces
       WHERE product_type ='TA' AND contract_month = '2014-08-01 00:00:00'::TIMESTAMP 
         WINDOW w AS (
          PARTITION BY date_trunc('second', ticktime)
          ORDER BY ticktime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
          )
      ) t USING (ticktime)



      ORDER BY 1 
    ) ;

2 个答案:

答案 0 :(得分:1)

假设此子查询想要生成一个time列,其中包含区间中每秒的行数:

  SELECT generate_series(
    date_trunc('second', min(ticktime)) ,
    date_trunc('second', max(ticktime)) ,
    interval '1 sec'
  ) FROM czces  AS g (time)

这是错误的,因为具有此语法的g(time)应该引用czces,而不是输出列。

你想要这个:

  SELECT generate_series(
    date_trunc('second', min(ticktime)) ,
    date_trunc('second', max(ticktime)) ,
    interval '1 sec'
  ) AS "time" FROM czces;

答案 1 :(得分:0)

如果您在) AS ticktime FROM czces ) x

中遗漏了别名 x

你将失去别名erorr

    ERROR:  subquery in FROM must have an alias
    LINE 6: (
            ^
    HINT:  For example, FROM (SELECT ...) [AS] foo.

可行版本(但仍不知道如何使用默认值填充NULL值)

    SELECT 
           x.ticktime,
           t.high,
           t.low
    FROM 
    (

      SELECT generate_series(
        date_trunc('second', min(ticktime)) ,
        date_trunc('second', max(ticktime)) ,
        interval '1 sec'
      ) AS ticktime FROM czces  ) x

      LEFT JOIN
      (
        SELECT 
          date_trunc('second', ticktime) AS ticktime ,
          max(last_price) OVER w AS high ,
          min(last_price) OVER w AS low 
       FROM czces
       WHERE product_type ='TA' AND contract_month = '2014-08-01 00:00:00'::TIMESTAMP 
         WINDOW w AS (
          PARTITION BY date_trunc('second', ticktime)
          ORDER BY ticktime ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
          )
      ) t USING (ticktime)


      ORDER BY 1 
    ;