如何使用条件计算sql窗口分区中的时间差

发布于 2025-01-09 12:11:14 字数 8 浏览 1 评论 0原文

continue

Problem

I have a table which lists series of events as "fetch" and click". User can perform multiple searches in 60 seconds. The tasks need to be sequenced so that user is performing a fetch and click within 60 seconds or multiple fetches within 60 seconds of each other and then clicking

  1. Fetch -> Click (user found the answer within 60 seconds )
  2. Fetch -> Fetch ->...Click (user didn't find the answer and kept searching until found the answer and then clicked within 60 seconds)
  3. Fetch -> no action (user didn't perform any further action after first fetch)

What I tried in the code below is to partition first on user_id, time as hour(hh) and time as minute(mm) to see the events that occurred within 60 seconds of each other but same hour and minute

I'm getting stuck at calculating how many clicks and fetches exist in a partition. I need to produce Output table by checking the conditions that

  1. If there is only 1 fetch and 1 click in a partition then flag as NA

  2. If (count(fetch) >=2 and count(click) = 1) and click is within 60 seconds of first fetch by the user then flag as Clicked and calculate the time between click and first fetch

  3. If there is only 1 fetch and no subsequent click, then flag it as Not-Clicked and calculate the time between this fetch and next fetch

Output table shows the desired data and columns.

Select a.window, a.user_id, a.search_id, a.date, a.time, a.action,

    count(case when a.action = 'click' then 1 else 0 end)  
       over(Partition BY  a.user_id, a.hh, a.mm ORDER BY  a.user_id asc, a.hh asc, a.mm asc) as total_clicks ,

    count(case when a.action = 'fetch' then 1 else 0 end) as total_fetches 
       over(Partition BY  a.user_id, a.hh, a.mm ORDER BY  a.user_id asc, a.hh asc, a.mm asc) as total_fetches 
From

(

    SELECT
        a.user_id, a.search_id, a.date, a.time, a.action, hour(a.time) as hh, minute(a.time) as mm,
        dense_rank() OVER ( ORDER BY  a.user_id asc, hour(a.time) asc, minute(a.time asc)) as window
     

    From input_table a

) as a

Input Table

windowuser_idsearch_iddatetimeaction
1u1s11/4/2201/04/2022 10:05:10fetch
1u1s11/4/2201/04/2022 10:05:17click
2u1s11/4/2201/04/2022 10:16:02fetch
2u1s21/4/2201/04/2022 10:16:32fetch
2u1s21/4/2201/04/2022 10:16:37click
5u3s71/4/2201/04/2022 04:36:00fetch
6u3s81/4/2201/04/2022 04:37:00fetch
6u3s81/4/2201/04/2022 04:37:01fetch
6u3s81/4/2201/04/2022 04:37:04click

Desired Output

windowuser_idsearch_iddatetimeactionflagclicked_timeleft_time
1u1s11/4/2201/04/2022 10:05:10fetchNA
1u1s11/4/2201/04/2022 10:05:17clickNA
2u1s11/4/2201/04/2022 10:16:02fetchClicked
2u1s21/4/2201/04/2022 10:16:32fetchClicked
2u1s21/4/2201/04/2022 10:16:37clickClicked35
5u3s71/4/2201/04/2022 04:36:00fetchNot-Clicked60
6u3s81/4/2201/04/2022 04:37:00fetchClicked
6u3s81/4/2201/04/2022 04:37:01fetchClicked
6u3s81/4/2201/04/2022 04:37:04clickClicked04

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文