SUM all timeelapse WHERE timetype =' Break'但只有时间类型='开始'之后的行?

时间:2015-06-28 05:45:18

标签: postgresql function sum

))大家好,这是我的桌子......

enter image description here

这是用于创建同一个表的SQL语句...

CREATE TABLE tbl_ebscbspa_log05
(
  pcnum smallint NOT NULL,
  fnserial serial NOT NULL,
  fnname character varying NOT NULL,
  timestmp timestamp without time zone DEFAULT clock_timestamp(),
  timeelapse interval,
  timetype character varying,
  timeindex real,
  CONSTRAINT table_ebscb_spa_log05_pkey PRIMARY KEY (fnserial)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE tbl_ebscbspa_log05
  OWNER TO postgres;

我怎样才能使所有时间都消失?时间类型='休息'并且放在最后(下方)时间类型之后(下方)='开始'?所以在上面的表格中,我得到了两个蓝色方形彩色行。所以我得到了#wotalbreak' = 00-00-00 02:00:00.000

如何选择所有可能的语法' Breaks'在最后(后代)'开始'总结一下? (假设最大可能'休息'是九)。

我试过这个,结果是NULL

SELECT t.timeelapse FROM tbl_ebscbspa_log05 t WHERE t.timetype = 'Break' AND NOT EXISTS (SELECT 1 FROM tbl_ebscbspa_log05 WHERE timetype = 'Start' AND 'timestmp' > 't.timestmp') INTO v_timeelapse_break;
        IF FOUND THEN
          NEW.timeelapse := v_timeelapse_break;

请问我们如何理解他们,而不仅仅是SELECT THEM,希望一些优秀的PostgreSQL同事程序员可以帮助我。

谢谢高级。

1 个答案:

答案 0 :(得分:0)

SELECT sum(timeelapse)
FROM tbl_ebscbspa_log05
WHERE timetype = 'Break'
  AND fnserial > 
    /* Get serial of last record with timetyoe 'Start' */
   ( SELECT max( fnserial )
      FROM tbl_ebscbspa_log05
      WHERE timetype = 'Start'
    )