使用Amazon Redshift中的max()或min()条件查询优化选择

时间:2018-12-04 13:04:48

标签: sql max amazon-redshift min

现在我有一个看起来像这样的表:

@Data
@AllArgsConstructor
public class User {
  private String email;
  private long flags;

  public static long merge(long... flags) {
    long result = 0;
    for (long flag : flags) {
      result = result | flag;
    }
    return result;
  }


  // test
  public static void main(String[] args) {
    User user1 = new User("email@gmail.com", Long.valueOf("1000000000000101",2));
    User user2 = new User("email@gmail.com", Long.valueOf("0000111100000101",2));
    User user3 = new User("email@gmail.com", Long.valueOf("0000000010110101",2));
    System.out.println(Long.toBinaryString(merge(user1.flags, user2.flags, user3.flags)));
    // result is 1000111110110101
  }
}

我要做的是,总是提取访问者在我们网站上所做的最后操作。

我当前的解决方案是将表格分成2个,获得最大的点击数,然后结合剩余信息(即

VisitorId   VisitId   visittime   hitNumber    Pagetitle  EventCategory
1001        0001      2017-12-31    1          Product    scroll down 
1001        0001      2017-12-31    2          Product    click   
1001        0001      2017-12-31    3          Product    Fill info
1002        0002      2017-12-31    1          Homepage   Download
1002        0002      2017-12-31    2          Contact    /
1002        0002      2017-12-31    3          Check out  Shopping cart
1002        0002      2017-12-31    4          Check out  Abandon shopping cart

但是我很好奇是否还有其他选择可以使脚本更短且效果相同。

有人对此有想法吗?非常感谢您的提前帮助!

1 个答案:

答案 0 :(得分:0)

尝试窗口功能:

select t.*
from (select t.*,
             row_number() over (partition by visitor_id, visit_id order by hit_number desc) as seqnum
      from table t
     ) t
where seqnum = 1;