简体   繁体   English

在连续的Postgresql行中查找数据模式

[英]Finding data patterns in sequential Postgresql rows

I'd like to ask Postgres how often two occurrences of an event, one occurrence per row, are seen. 我想问一下Postgres,一次出现两次事件(每行一次)的频率是多少。 For example, if I have user events like: 例如,如果我有以下用户事件:

  • User 1: Clicked button 1, redirected to page 2 用户1:单击按钮1,重定向到页面2
  • User 1: Clicked button 2, redirected to page 3 用户1:单击按钮2,重定向到页面3
  • User 1: Clicked button 18, redirected to page 100 用户1:单击按钮18,重定向到页面100
  • User 1: Clicked button 1, redirected to page 2 用户1:单击按钮1,重定向到页面2
  • User 1: Clicked button 2, redirected to page 3 用户1:单击按钮2,重定向到页面3

then I would see the pattern ((button 1, page 2) => (button 2, page 3)) counted as two occurances. 那么我会看到模式((第2页的按钮1)=>(第3页的按钮2)被计为两次事件。

Is this possible, and if so, how? 这可能吗?如果可以,怎么办?

It's a very good question and has a fairly simple solution. 这是一个很好的问题,并且有一个相当简单的解决方案。 Use GROUP BY and HAVING to find out which user shows what sort of repeated behavior. 使用GROUP BYHAVING找出哪个用户显示了哪种重复行为。

Please see the fiddle example here which discusses the DDL and the query I have used to get the desired result. 请参见此处的小提琴示例该示例讨论了DDL和我用来获得所需结果的查询。

From your description I recommend you create a table for storing user events as follows: 根据您的描述,我建议您创建一个用于存储用户事件的表,如下所示:

CREATE TABLE t_clickevent (
   clickevent_id        INTEGER,
   user_id              INTEGER,
   clicked_button_id   INTEGER,
   redirected_url_id   INTEGER);

Add any more columns as and when you require. 在需要时添加更多列。 This is just a minimal structure. 这只是一个最小的结构。

Use the query as follows: 使用查询,如下所示:

SELECT user_id, clicked_button_id,
       redirected_url_id
  FROM t_clickevent
GROUP BY user_id, clicked_button_id,
       redirected_url_id
HAVING count(*) > 1;

Output: 输出:

USER_ID     CLICKED_BUTTON_ID     REDIRECTED_URL_ID
----------- --------------------- -----------------
1           1                     2
1           2                     3

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM