我的头在寻找以下解决方案... 我有一个有效的查询,该查询使我获得具有第一个create_date以及指定条件的行。现在,我想扩展该查询并加入另一个表事件,然后使用事件“注册”来分别过滤所有记录。
我想用我的查询回答的问题是哪个用户的参照路径为'/ community%',等等,然后进一步过滤其中哪些用户具有事件'signup'。我用工作查询解决的第一部分,但是如何重新定义它以仅基于该用户执行注册操作来取回用户?我在使用postgres。
我对第一部分的工作查询:
SELECT DISTINCT ON (cookie_hash) cookie_hash, crdate, http_referer, refererpath
FROM trace t1
WHERE crdate = (SELECT MIN(crdate) FROM trace t2 WHERE t1.id = t2.id)
AND refererpath LIKE '/community%' AND http_referer IS NOT NULL AND http_referer NOT LIKE 'https://example.io%'
ORDER BY cookie_hash, crdate
我尝试过的事情:
SELECT DISTINCT ON (cookie_hash) t1.cookie_hash, t1.crdate, t1.http_referer, t1.refererpath
FROM trace t1
INNER JOIN event on t1.cookie_hash = event.cookie_hash
WHERE t1.crdate = (SELECT MIN(crdate) FROM trace t2 WHERE t1.id = t2.id)
AND refererpath LIKE '/community%' AND http_referer IS NOT NULL AND http_referer NOT LIKE 'https://example.io%'
AND event.action = 'signup'
ORDER BY cookie_hash, crdate
这会给我返回错误的结果,其中包含在指定条件之前发生注册事件的跟踪:/
我的两个表具有以下结构:
表跟踪:
+--------------+-----------------------------+-----------------------------------------+
| Column | Type | Modifiers |
|--------------+-----------------------------+-----------------------------------------|
| id | integer | not null |
| cookie_hash | character varying(255) | default NULL::character varying |
| crdate | timestamp(0) with time zone | default NULL::timestamp with time zone |
| action | character varying(255) | default NULL::character varying |
| account_uuid | character varying(255) | default NULL::character varying |
+--------------+-----------------------------+-----------------------------------------+
和表事件:
+-----------------+-----------------------------+-----------------------------------------+
| Column | Type | Modifiers |
|-----------------+-----------------------------+-----------------------------------------|
| id | integer | not null |
| cookie_hash | character varying(255) | default NULL::character varying |
| crdate | timestamp(0) with time zone | default NULL::timestamp with time zone |
| remote_addr | character varying(255) | default NULL::character varying |
| refererhost | text | |
| refererpath | text | |
| http_referer | text | |
| account_uuid | character varying(255) | default NULL::character varying |
+-----------------+-----------------------------+-----------------------------------------+
以下是一些有关该问题的更多详细信息的示例数据:
我使用了Gordon Linoff的答案提供的查询...但是它也没有按预期工作,并不是我所有返回的数据都正确无误。
我的结果中的许多行都不包含第一个/最早的时间戳。 这是返回行,按预期方式运行:
| 3d16632fe65e2155db0bd3304bae8ad2 | 2019-01-31 15:01:38+00 | signup | https://www.google.com/ | /community/.... | |
当我在跟踪表中搜索此cookie_hash时,可以用以下证明:
+---------+----------------------------------+------------------------+---------------+--------+-----------------+-----------------+----------------+-------------------+----------------------------------------------------------+-------------------------+------------------------------------------------------------------------------------------------------------
| id | cookie_hash | crdate | remote_addr | uri | refererhost | refererscheme | refererquery | refererfragment | refererpath | http_referer | http_user_agent
|---------+----------------------------------+------------------------+---------------+--------+-----------------+-----------------+----------------+-------------------+----------------------------------------------------------+-------------------------+------------------------------------------------------------------------------------------------------------
| 1182240 | 3d16632fe65e2155db0bd3304bae8ad2 | 2019-01-31 15:01:38+00 | xxx | <null> | example.io | https | <null> | <null> | /community/.... | https://www.google.com/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Saf
| 1182241 | 3d16632fe65e2155db0bd3304bae8ad2 | 2019-01-31 15:01:47+00 | xxx | <null> | ....io | https | <null> | <null> | /signup/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Saf
| 1182248 | 3d16632fe65e2155db0bd3304bae8ad2 | 2019-01-31 15:04:52+00 | xxx | <null> | example.io | https | <null> | <null> | /community/.... | https://www.google.com/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/12.0.2 Saf
+---------+----------------------------------+------------------------+---------------+--------+-----------------+-----------------+----------------+-------------------+----------------------------------------------------------+-------------------------+------------------------------------------------------------------------------------------------------------
但是许多返回的行是不正确的,我得到了cookie_hashhes,它们没有像'/ community%'这样的第一命中,并且没有NULL的http_referer ...请看下面的示例行:
| 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 11:46:49+00 | signup | https://www.google.de/ | /community/.../.../ |
当我在跟踪表中查找此跟踪时,得到以下结果:
+--------+----------------------------------+------------------------+---------------------------------------+--------+-----------------+-----------------+----------------+-------------------+---------------------------------------------------+---------------------------------+------------------------------------------------------------------------------------
| id | cookie_hash | crdate | remote_addr | uri | refererhost | refererscheme | refererquery | refererfragment | refererpath | http_referer | http_user_agent
|--------+----------------------------------+------------------------+---------------------------------------+--------+-----------------+-----------------+----------------+-------------------+---------------------------------------------------+---------------------------------+------------------------------------------------------------------------------------
| 528636 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:03:12+00 | xxx | <null> | example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528637 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:03:19+00 | xxx | <null> | example.io | https | <null> | <null> | / | https://example.io/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528638 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:03:30+00 | xxx | <null> | example.io | https | <null> | <null> | /bla/team/ | https://example.io/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528690 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:24:47+00 | xxx | <null> | my.example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528692 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:26:01+00 | xxx | <null> | my.example.io | https | <null> | <null> | /signup/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528693 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:26:06+00 | xxx | <null> | my.example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528695 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:26:13+00 | xxx | <null> | my.example.io | https | <null> | <null> | /signup/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528700 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:26:41+00 | xxx | <null> | my.example.io | https | <null> | <null> | /xxx/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528701 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:27:17+00 | xxx | <null> | my.example.io | https | <null> | <null> | /ValidateSuccess | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528702 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:27:22+00 | xxx | <null> | my.example.io | https | <null> | <null> | /xxx/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528703 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:27:45+00 | xxx | <null> | my.example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528705 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:27:56+00 | xxx | <null> | example.io | https | <null> | <null> | /community/.../ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528721 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 09:30:45+00 | xxx | <null> | example.io | https | <null> | <null> | /bla/team/ | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528847 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 10:41:34+00 | xxx | <null> | example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528848 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 10:41:38+00 | xxx | <null> | example.io | https | <null> | <null> | /bla/team/ | https://example.io/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 528999 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 11:46:49+00 | xxx | <null> | example.io | https | <null> | <null> | /community/.../.../ | https://www.google.de/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 529016 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 11:57:00+00 | xxx | <null> | example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 529017 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 11:57:04+00 | xxx | <null> | example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 529171 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 13:15:59+00 | xxx | <null> | example.io | https | <null> | <null> | / | <null> | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 529172 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 13:16:02+00 | xxx | <null> | example.io | https | <null> | <null> | /bla/ | https://example.io/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
| 529173 | 9edf070706c8a728cc78719befc6cdd7 | 2018-01-08 13:16:04+00 | xxx | <null> | example.io | https | <null> | <null> | /bla/team/ | https://example.io/bla/ | Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/604.4.7 (KHTML, like Ge
+--------+----------------------------------+------------------------+---------------------------------------+--------+-----------------+-----------------+----------------+-------------------+---------------------------------------------------+---------------------------------+------------------------------------------------------------------------------------
答案 0 :(得分:1)
您要在distinct on
之后的之后进行过滤吗?
SELECT x.*
FROM (SELECT DISTINCT ON (cookie_hash) t1.cookie_hash, t1.crdate, event.action, t1.http_referer, t1.refererpath
FROM (SELECT t1.*, MIN(crdate) OVER (PARTITION BY id) as min_crdate
FROM trace t1
) t1 INNER JOIN
event
ON t1.cookie_hash = event.cookie_hash
WHERE t1.crdate = t1.min_crdate AND
refererpath LIKE '/community%' AND
http_referer IS NOT NULL AND
http_referer NOT LIKE 'https://example.io%'
ORDER BY cookie_hash, crdate;
) x
WHERE action = 'signup'