如果使用语句postgresql,如何正确使用

问题描述 投票:0回答:1

我正在尝试创建一个触发器,每当X数量的行添加到其他表时,该触发器就会填充一个表。逻辑将是:

1- check how many rows are in table A based on last date on table B.
2- once there are 1000 rows in table A from the last date on table B, add one row to table B

表A存储来自市场的报价,表B存储OHLC数据(开盘,最高价,最低价,收盘价)。报价只有3栏:日期,出价和要价。

Open是第一个已知价格,Low是最小价格,High是最大价格,Close是最后一个价格。所有这些价格都是根据使用出价列的最后1000个报价计算得出的。它将是:

  • 公开:出价n°0
  • 最高:最高出价介于0到999之间
  • 低:最低出价介于0到999之间
  • 关闭:出价999

事实是,我正在尝试检查是否有足够的滴答声来创建蜡烛,并且我正在尝试构造这样的触发器:

with total_ticks as (
    select count(*) from (
    select *  from eurusd_tick2 eurusd where date > 
        (SELECT date from eurusd_ohlc order by date desc limit 1) 
        order by date asc) totals)

此代码实际上为我提供了自上次已知日期以来的总变动量。问题来了,因为我试图使用“ if”条件语句来开始构建逻辑,但是我只会遇到语法错误。

例如,我尝试过

if 1000<total_ticks then 
   raise notice 'there are less than 1000'

if 1000 < select * from total_ticks then
   raise notice 'there are less than 1000'

我应该如何使用if语句?我在这里想念的是什么?我也尝试过类似的事情:

DO $$
DECLARE
  a integer := 10;
  b integer := 20;
BEGIN 
  IF a > b THEN
    RAISE NOTICE 'a is greater than b';
  END IF;

  IF a < b THEN
    RAISE NOTICE 'a is less than b';
  END IF;

  IF a = b THEN
    RAISE NOTICE 'a is equal to b';
  END IF;
END $$;

但是我收到错误消息,指出“ DO”附近或附近有错误。

我很困惑,所以在使用此功能方面会有所帮助。

sql postgresql finance trading candlestick-chart
1个回答
0
投票

使其简短一些:

do $$
declare
  a integer := 10;
  b integer := 20;
  msg text;
begin
  msg := case
    when a>b then 'a is greater than b'
    when a<b then 'a is less than b'
    when a=b then 'a is equal to b'
  end;
  raise notice '%', msg;
end $$;

显然,它在psql中有效。 finance trading candlestick-chart

可能有问题
© www.soinside.com 2019 - 2024. All rights reserved.