统计基于ID和状态码的数据块数。

huangapple go评论56阅读模式
英文:

Count blocks of data based on id and status code

问题

id status datetime block
123456 0 07/02/2023 12:43 1
123456 4 07/02/2023 12:49 1
123456 5 07/02/2023 12:58 1
123456 5 07/02/2023 13:48 1
123456 7 07/02/2023 14:29 1
123456 0 07/02/2023 14:50 2
123456 4 07/02/2023 14:50 2
123456 5 07/02/2023 14:51 2
123456 9 07/02/2023 15:27 2
567890 0 07/02/2023 11:44 1
567890 4 07/02/2023 12:23 1
567890 5 07/02/2023 12:29 1
567890 5 07/02/2023 13:26 1
567890 5 07/02/2023 13:28 1
567890 5 07/02/2023 13:28 1
567890 5 07/02/2023 13:29 1
567890 9 07/02/2023 13:55 1
英文:

I have a dataset that looks something like this

id status datetime
123456 0 07/02/2023 12:43
123456 4 07/02/2023 12:49
123456 5 07/02/2023 12:58
123456 5 07/02/2023 13:48
123456 7 07/02/2023 14:29
123456 0 07/02/2023 14:50
123456 4 07/02/2023 14:50
123456 5 07/02/2023 14:51
123456 9 07/02/2023 15:27
567890 0 07/02/2023 11:44
567890 4 07/02/2023 12:23
567890 5 07/02/2023 12:29
567890 5 07/02/2023 13:26
567890 5 07/02/2023 13:28
567890 5 07/02/2023 13:28
567890 5 07/02/2023 13:29
567890 9 07/02/2023 13:55

For each id in the dataset there are a number of statuses that need to be identified as 'blocks' of activity, where each block starts with a status code of 0 (and is sorted by datetime)

What I'd like to do is to add a column that identifies this block. So my data would look like this with that column added.

id status datetime block
123456 0 07/02/2023 12:43 1
123456 4 07/02/2023 12:49 1
123456 5 07/02/2023 12:58 1
123456 5 07/02/2023 13:48 1
123456 7 07/02/2023 14:29 1
123456 0 07/02/2023 14:50 2
123456 4 07/02/2023 14:50 2
123456 5 07/02/2023 14:51 2
123456 9 07/02/2023 15:27 2
567890 0 07/02/2023 11:44 1
567890 4 07/02/2023 12:23 1
567890 5 07/02/2023 12:29 1
567890 5 07/02/2023 13:26 1
567890 5 07/02/2023 13:28 1
567890 5 07/02/2023 13:28 1
567890 5 07/02/2023 13:29 1
567890 9 07/02/2023 13:55 1

I've used window functions before, but I can't get my head around how to do this.

答案1

得分: 1

以下是翻译好的代码部分:

你可以使用CTE和以下查询来获得所需的结果

样本表:

    CREATE TABLE SampleData (
      id INT,
      status INT,
      datetime DATETIME
    );

样本数据:

    INSERT INTO SampleData (id, status, datetime)
    VALUES
      (123456, 0, '07/02/2023 12:43'),
      (123456, 4, '07/02/2023 12:49'),
      (123456, 5, '07/02/2023 12:58'),
      (123456, 5, '07/02/2023 13:48'),
      (123456, 7, '07/02/2023 14:29'),
      (123456, 0, '07/02/2023 14:50'),
      (123456, 4, '07/02/2023 14:50'),
      (123456, 5, '07/02/2023 14:51'),
      (123456, 9, '07/02/2023 15:27'),
      (567890, 0, '07/02/2023 11:44'),
      (567890, 4, '07/02/2023 12:23'),
      (567890, 5, '07/02/2023 12:29'),
      (567890, 5, '07/02/2023 13:26'),
      (567890, 5, '07/02/2023 13:28'),
      (567890, 5, '07/02/2023 13:28'),
      (567890, 5, '07/02/2023 13:29'),
      (567890, 9, '07/02/2023 13:55');


查询:

      WITH CteSampleData AS (
      SELECT *,
             SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END)
             OVER (PARTITION BY id ORDER BY datetime) AS block
      FROM SampleData
    )
    SELECT id, status, datetime, block
    FROM CteSampleData
    ORDER BY id, datetime;
英文:

You may get the desired result using CTE and the below query

Sample Table:

CREATE TABLE SampleData (
  id INT,
  status INT,
  datetime DATETIME
);

Sample Data:

INSERT INTO SampleData (id, status, datetime)
VALUES
  (123456, 0, '07/02/2023 12:43'),
  (123456, 4, '07/02/2023 12:49'),
  (123456, 5, '07/02/2023 12:58'),
  (123456, 5, '07/02/2023 13:48'),
  (123456, 7, '07/02/2023 14:29'),
  (123456, 0, '07/02/2023 14:50'),
  (123456, 4, '07/02/2023 14:50'),
  (123456, 5, '07/02/2023 14:51'),
  (123456, 9, '07/02/2023 15:27'),
  (567890, 0, '07/02/2023 11:44'),
  (567890, 4, '07/02/2023 12:23'),
  (567890, 5, '07/02/2023 12:29'),
  (567890, 5, '07/02/2023 13:26'),
  (567890, 5, '07/02/2023 13:28'),
  (567890, 5, '07/02/2023 13:28'),
  (567890, 5, '07/02/2023 13:29'),
  (567890, 9, '07/02/2023 13:55');

Query:

  WITH CteSampleData AS (
  SELECT *,
         SUM(CASE WHEN status = 0 THEN 1 ELSE 0 END)
         OVER (PARTITION BY id ORDER BY datetime) AS block
  FROM SampleData
)
SELECT id, status, datetime, block
FROM CteSampleData
ORDER BY id, datetime;

huangapple
  • 本文由 发表于 2023年2月8日 18:26:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75384399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定