HAWQ PostgreSQL-根据上一行增加行

时间:2018-08-15 19:16:34

标签: sql postgresql datatable psql hawq

我需要从此table1创建一个table2,以尝试更新下表:

@Documented
@Constraint(validatedBy = UserValidator.class)
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface UserConstraint {
   String message() default "Invalid age";
   Class<?>[] groups() default {};
   Class<? extends Payload>[] payload() default {};
}

尝试根据以前的值(例如Excel中的Ctrl + D)填充“事件”列

public class UserValidator implements ConstraintValidator<UserConstraint, Integer>  {

        @Override
        public void initialize(UserConstraint constraintAnnotation) {
        }

        @Override
        public boolean isValid(Integer age, ConstraintValidatorContext context) {

          if(age > 60) {
              //Here I want to return HTTP CODE 402 to the user 
              return false;
          } else if(age < 18) {
              //Here I want to return HTTP CODE 422 to the user 
              return false;
          }
          return true;  
    }
}

问题是事件未遵循顺序并且count(ID,Rank)也不是恒定的。 我不能尝试使用基于变量的函数,因为它具有数百万条记录,也不能使用'update'作为其Hawq。

有什么建议吗? 谢谢!

3 个答案:

答案 0 :(得分:1)

您可以尝试将max窗口功能

一起使用
CREATE TABLE T(ID int, Rank int, Event varchar(50));


INSERT INTO T VALUES (123456, 1,'178');
INSERT INTO T VALUES (123456, 2,'');
INSERT INTO T VALUES (123456, 3,'');
INSERT INTO T VALUES (123456, 4,'');
INSERT INTO T VALUES (123456, 5,'');
INSERT INTO T VALUES (123456, 6,'');
INSERT INTO T VALUES (123456, 7,'');
INSERT INTO T VALUES (356589, 1,'165');
INSERT INTO T VALUES (356589, 2,'');
INSERT INTO T VALUES (356589, 3,'');
INSERT INTO T VALUES (356589, 4,'');
INSERT INTO T VALUES (565984, 1,'1025');
INSERT INTO T VALUES (565984, 2,'');
INSERT INTO T VALUES (987456, 1,'85');
INSERT INTO T VALUES (987456, 2,'');
INSERT INTO T VALUES (987456, 3,'');
INSERT INTO T VALUES (987456, 4,'');
INSERT INTO T VALUES (987456, 5,'');
INSERT INTO T VALUES (987456, 6,'');

查询1

SELECT t.id,t.rank,max(Event) over (partition by ID order by Rank) 
FROM T

Results

|     id | rank |  max |
|--------|------|------|
| 123456 |    1 |  178 |
| 123456 |    2 |  178 |
| 123456 |    3 |  178 |
| 123456 |    4 |  178 |
| 123456 |    5 |  178 |
| 123456 |    6 |  178 |
| 123456 |    7 |  178 |
| 356589 |    1 |  165 |
| 356589 |    2 |  165 |
| 356589 |    3 |  165 |
| 356589 |    4 |  165 |
| 565984 |    1 | 1025 |
| 565984 |    2 | 1025 |
| 987456 |    1 |   85 |
| 987456 |    2 |   85 |
| 987456 |    3 |   85 |
| 987456 |    4 |   85 |
| 987456 |    5 |   85 |
| 987456 |    6 |   85 |

答案 1 :(得分:1)

您可以使用FIRST_VALUE

SELECT ID, RANK, 
  FIRST_VALUE(Event) OVER(PARTITION BY ID ORDER BY Rank) AS Event
FROM tab;

编辑:

  

道歉!每个ID都有多个事件代码。

您可以通过其他分组进行处理:

WITH cte AS (
  SELECT ID, RANK, EVENT,
    SUM(CASE WHEN event IS NULL THEN 0 ELSE 1 END) 
        OVER(PARTITION BY ID ORDER BY RANK) AS grp
  FROM t
)
SELECT ID, RANK,
   FIRST_VALUE(Event) OVER(PARTITION BY ID, grp ORDER BY Rank) AS Event
FROM cte;

Rextester Demo

答案 2 :(得分:0)

使用数组操作可以在没有子查询的情况下执行此操作:

select v.*,
       (array_remove(array_agg(event) over (partition by id order by rank), NULL))[count(event) over (partition by id order by rank)]
from (values (1, 'a', 15),
             (2, 'a', null),
             (3, 'a', null),
             (4, 'a', 20),
             (5, 'a', null),
             (1, 'b', 4),
             (2, 'a', null)
     ) v(rank, id, event)
order by id, rank