0

我需要按用户计算 IssueID 的声明时间总声明时间是从状态声明到最近等待的时间。看起来有点复杂请帮忙。

 IssueID    TransTime   User    Status
101 2019-08-23 0:25:41  Peter   CLAIMED
101 2019-08-23 0:25:44  Peter   CLAIMED
101 2019-08-23 0:26:12  Peter   WAITING
101 2019-08-23 20:14:13 Peter   CLAIMED
101 2019-08-23 20:14:16 Peter   CLAIMED
101 2019-08-23 20:14:52 Peter   WAITING
102 2019-08-24 8:59:19  Miller  CLAIMED
102 2019-08-24 8:59:56  Miller  CLAIMED
102 2019-08-24 9:00:09  Miller  WAITING
102 2019-08-24 9:00:17  Miller  CLAIMED
102 2019-08-24 9:00:20  Miller  CLAIMED
102 2019-08-25 21:56:52 Miller  WAITING`

例如,对于 peter,总索赔时间从 '2019-08-23 0:25:41' 到第一个等待时间 '2019-08-23 0:26:12' 和下一个从 '2019-08-23 20' 开始: 14:13' 到 '2019-08-23 20:14:52'。所有这些时间差加起来就是彼得声称的总时间,第一次大约是 31 秒,第二次大约是 39 秒。大约 70 秒。

提前致谢

`

4

2 回答 2

0

以下是 BigQuery 标准 SQL

#standardSQL
WITH `project.dataset.table` AS (
  SELECT 101 IssueID, TIMESTAMP '2019-08-23 0:25:41' TransTime, 'Peter' User, 'CLAIMED' Status UNION ALL
  SELECT 101, '2019-08-23 0:25:44', 'Peter', 'CLAIMED' UNION ALL
  SELECT 101, '2019-08-23 0:26:12', 'Peter', 'WAITING' UNION ALL
  SELECT 101, '2019-08-23 20:14:13', 'Peter', 'CLAIMED' UNION ALL
  SELECT 101, '2019-08-23 20:14:16', 'Peter', 'CLAIMED' UNION ALL
  SELECT 101, '2019-08-23 20:14:52', 'Peter', 'WAITING' UNION ALL
  SELECT 102, '2019-08-24 8:59:19', 'Miller', 'CLAIMED' UNION ALL
  SELECT 102, '2019-08-24 8:59:56', 'Miller', 'CLAIMED' UNION ALL
  SELECT 102, '2019-08-24 9:00:09', 'Miller', 'WAITING' UNION ALL
  SELECT 102, '2019-08-24 9:00:17', 'Miller', 'CLAIMED' UNION ALL
  SELECT 102, '2019-08-24 9:00:20', 'Miller', 'CLAIMED' UNION ALL
  SELECT 102, '2019-08-25 21:56:52', 'Miller', 'WAITING' 
)
SELECT IssueID, SUM(waiting_time) total_waiting_time 
FROM (
  SELECT IssueID, TIMESTAMP_DIFF(MAX(TransTime), MIN(TransTime), SECOND) waiting_time
  FROM (
    SELECT *, COUNTIF(start) OVER(PARTITION BY IssueID ORDER BY TransTime) waiting
    FROM (
      SELECT *, ('CLAIMED' = status AND IFNULL(LAG(status) OVER(PARTITION BY IssueID ORDER BY TransTime), 'WAITING') = 'WAITING') start
      FROM `project.dataset.table`
      WHERE status IN ('CLAIMED', 'WAITING')
    )
  )
  GROUP BY IssueID, waiting
)
GROUP BY IssueID
ORDER BY IssueID

结果

Row IssueID total_waiting_time   
1   101     70   
2   102     133045   
于 2019-09-23T18:30:25.670 回答
0

您可以通过计算每行之后的“等待”次数来识别每个组。然后使用此信息获取每个索赔期。所以:

select issueId,
       min(transTime) as min_time,
       max(transTime) as max_time),
       datetime_diff(min(transTime), max(transTime), second) as time_in_seconds
from (select t.*,
             countif(status = 'WAITING') over (partition by issueId order by transTime desc) as grp
      from t
      where status in ('WAITING', 'CLAIM')
     ) t
group by issueId, grp;

我不确定是否正是您想要的——您可能需要额外的聚合级别。但这是计算每个时期的想法。

于 2019-09-23T10:55:01.387 回答