在 sql 中计算每个用户在一个问题上花费的时间
Calculate the Time spent per user in a issue in sql
我有一个 table 有多个用户在工作,我需要计算总时间
根据声明的状态每位用户花费
ISSUEID Time User date status
10101 2019-08-30 19:51:43 peter 2019-08-30 CLAIMED
10101 2019-08-30 19:51:46 peter 2019-08-30 CLAIMED
10101 2019-08-30 20:10:39 miller 2019-08-30 CLAIMED
10101 2019-08-30 21:21:14 miller 2019-08-30 CLAIMED
10101 2019-08-30 21:22:33 Renus 2019-08-30 CLAIMED
10101 2019-08-31 1:31:58 Renus 2019-08-31 CLAIMED
10101 2019-08-31 1:32:08 peter 2019-08-31 CLAIMED
10101 2019-08-31 1:32:12 peter 2019-08-31 CLAIMED
10101 2019-08-31 1:32:58 miller 2019-08-31 CLAIMED
10101 2019-09-04 23:16:24 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:16:26 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:16:50 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:17:01 Renus 2019-09-04 CLAIMED
10101 2019-09-04 23:17:06 Renus 2019-09-04 CLAIMED
想要的结果
IssueID user timespent
10101 Peter 33
10101 Miller 4285
10101 Renus 30
使用 DATETIME_DIFF()
并在此之前使用 DATETIME_TRUNC
将日期时间转换为日期
SELECT
ISSUEID,
User,
DATETIME_DIFF(
min(DATETIME_TRUNC(Time, DAY)),
max(day),
day)
FROM table
GROUP BY ISSUEID, User
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT ISSUEID, user, SUM(timespent) timespent
FROM (
SELECT ISSUEID, user, TIMESTAMP_DIFF(MAX(time), MIN(time), SECOND) timespent
FROM (
SELECT *, COUNTIF(start) OVER(ORDER BY time) grp
FROM (
SELECT *, IFNULL(LAG(User) OVER(ORDER BY time), '') != User AS start
FROM `project.dataset.table`
)
)
GROUP BY ISSUEID, user, grp
)
GROUP BY ISSUEID, user
我有一个 table 有多个用户在工作,我需要计算总时间 根据声明的状态每位用户花费
ISSUEID Time User date status
10101 2019-08-30 19:51:43 peter 2019-08-30 CLAIMED
10101 2019-08-30 19:51:46 peter 2019-08-30 CLAIMED
10101 2019-08-30 20:10:39 miller 2019-08-30 CLAIMED
10101 2019-08-30 21:21:14 miller 2019-08-30 CLAIMED
10101 2019-08-30 21:22:33 Renus 2019-08-30 CLAIMED
10101 2019-08-31 1:31:58 Renus 2019-08-31 CLAIMED
10101 2019-08-31 1:32:08 peter 2019-08-31 CLAIMED
10101 2019-08-31 1:32:12 peter 2019-08-31 CLAIMED
10101 2019-08-31 1:32:58 miller 2019-08-31 CLAIMED
10101 2019-09-04 23:16:24 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:16:26 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:16:50 peter 2019-09-04 CLAIMED
10101 2019-09-04 23:17:01 Renus 2019-09-04 CLAIMED
10101 2019-09-04 23:17:06 Renus 2019-09-04 CLAIMED
想要的结果
IssueID user timespent
10101 Peter 33
10101 Miller 4285
10101 Renus 30
使用 DATETIME_DIFF()
并在此之前使用 DATETIME_TRUNC
SELECT
ISSUEID,
User,
DATETIME_DIFF(
min(DATETIME_TRUNC(Time, DAY)),
max(day),
day)
FROM table
GROUP BY ISSUEID, User
以下适用于 BigQuery 标准 SQL
#standardSQL
SELECT ISSUEID, user, SUM(timespent) timespent
FROM (
SELECT ISSUEID, user, TIMESTAMP_DIFF(MAX(time), MIN(time), SECOND) timespent
FROM (
SELECT *, COUNTIF(start) OVER(ORDER BY time) grp
FROM (
SELECT *, IFNULL(LAG(User) OVER(ORDER BY time), '') != User AS start
FROM `project.dataset.table`
)
)
GROUP BY ISSUEID, user, grp
)
GROUP BY ISSUEID, user