Teradata使用Intervals对记录进行分组查询

时间:2017-05-11 20:51:40

标签: sql teradata

在Teradata SQL中,如何为在8秒的时间间隔内创建的记录组分配相同的行号。

实施例: -

Customerid Customername   Itembought   dateandtime
                                       (yyy-mm-dd hh:mm:ss)
100           ALex        Basketball   2017-02-10 10:10:01
100           ALex        Circketball  2017-02-10 10:10:06
100           ALex        Baseball     2017-02-10 10:10:08
100           ALex        volleyball   2017-02-10 10:11:01
100          ALex         footbball    2017-02-10 10:11:05
100          ALex         ringball     2017-02-10 10:11:08
100         Alex          football     2017-02-10 10:12:10

我的预期结果应该包含Row_number的附加列,它应该在8秒内为客户的所有购买分配相同的数字:请参阅以下预期结果

Customerid Customername Itembought dateandtime            Row_number
                                   (yyy-mm-dd hh:mm:ss) 
100           ALex      Basketball 2017-02-10 10:10:01      1
100           ALex      Circketball 2017-02-10 10:10:06     1
100           ALex      Baseball    2017-02-10 10:10:08     1
100          ALex       volleyball  2017-02-10 10:11:01     2
100          ALex       footbball   2017-02-10 10:11:05     2
100          ALex       ringball    2017-02-10 10:11:08     2
100         Alex        football    2017-02-10 10:12:10     3

3 个答案:

答案 0 :(得分:0)

这是使用递归cte执行此操作的一种方法。当它获得>时,重置上一行的时间戳的差异的运行总和。 8到0并开始一个新的小组。

WITH ROWNUMS AS
  (SELECT T.*
         ,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY TM) AS RNUM 
         /*Replace DATEDIFF with Teradata specific function*/
         ,DATEDIFF(SECOND,COALESCE(MIN(TM) OVER(PARTITION BY ID
                                                ORDER BY TM ROWS BETWEEN 1 PRECEDING AND CURRENT ROW), TM),TM) AS DIFF
   FROM T --replace this with your tablename and add columns as required
   )
,RECURSIVE CTE(ID,TM,DIFF,SUM_DIFF,RNUM,GRP) AS
  (SELECT ID,
          TM,
          DIFF,
          DIFF,
          RNUM,
          CAST(1 AS int)
   FROM ROWNUMS
   WHERE RNUM=1
     UNION ALL
   SELECT T.ID,
          T.TM,
          T.DIFF,
          CASE WHEN C.SUM_DIFF+T.DIFF > 8 THEN 0 ELSE C.SUM_DIFF+T.DIFF END,
          T.RNUM,
          CAST(CASE WHEN C.SUM_DIFF+T.DIFF > 8 THEN T.RNUM ELSE C.GRP END AS int)
   FROM CTE C
   JOIN ROWNUMS T ON T.RNUM=C.RNUM+1 AND T.ID=C.ID
   )
SELECT ID,
       TM,
       DENSE_RANK() OVER(PARTITION BY ID ORDER BY GRP) AS row_num
FROM CTE

Demo in SQL Server

答案 1 :(得分:0)

使用Teradata的PERIOD数据类型和令人敬畏的td_normalize_overlap_meet

考虑表test32

 SELECT * FROM test32

+----+----+------------------------+
| f1 | f2 |           f3           |
+----+----+------------------------+
|  1 |  2 | 2017-05-11 03:59:00 PM |
|  1 |  3 | 2017-05-11 03:59:01 PM |
|  1 |  4 | 2017-05-11 03:58:58 PM |
|  1 |  5 | 2017-05-11 03:59:26 PM |
|  1 |  2 | 2017-05-11 03:59:28 PM |
|  1 |  2 | 2017-05-11 03:59:46 PM |
+----+----+------------------------+

以下内容将对您的记录进行分组:

WITH 
normalizedCTE AS
    (
        SELECT *
        FROM TABLE
            (
                td_normalize_overlap_meet(NEW VARIANT_TYPE(periodCTE.f1), periodCTE.fper)
                RETURNS (f1 integer, fper PERIOD(TIMESTAMP(0)), recordCount integer)
                HASH BY f1
                LOCAL ORDER BY f1, fper
            ) as output(f1, fper, recordcount)
    ),
periodCTE AS 
    (
        SELECT f1, f2, f3, PERIOD(f3, f3 + INTERVAL '9' SECOND) as fper FROM test32
    )


SELECT t2.f1, t2.f2, t2.f3, t1.fper, DENSE_RANK() OVER (PARTITION BY t2.f1 ORDER BY t1.fper) as fgroup
FROM normalizedCTE t1
    INNER JOIN periodCTE t2 ON
        t1.fper P_INTERSECT t2.fper IS NOT NULL

结果:

+----+----+------------------------+-------------+
| f1 | f2 |           f3           | fgroup      |
+----+----+------------------------+-------------+
|  1 |  2 | 2017-05-11 03:59:00 PM |           1 |
|  1 |  3 | 2017-05-11 03:59:01 PM |           1 |
|  1 |  4 | 2017-05-11 03:58:58 PM |           1 |
|  1 |  5 | 2017-05-11 03:59:26 PM |           2 |
|  1 |  2 | 2017-05-11 03:59:28 PM |           2 |
|  1 |  2 | 2017-05-11 03:59:46 PM |           3 |
+----+----+------------------------+-------------+

Teradata中的Period是一种特殊数据类型,它包含日期或日期时间范围。第一个参数是范围的开始,第二个参数是结束时间(最多但不包括,这是为什么它是“+ 9秒”)。结果是我们获得了8秒的“时间段”,其中每条记录可能与另一条记录“相交”。

然后我们使用td_normalize_overlap_meet合并相交的记录,共享f1字段的值作为键。在你的情况下,customerid。结果是这一个客户的三条记录,因为我们有三个组“重叠”或“相互”相互的时间段。

然后我们将td_normalize_overlap_meet输出与我们确定周期时的输出结合起来。我们使用P_INTERSECT函数来查看归一化CTE INTERSECT中哪些时段与初始时段CTE中的时段。从P_INTERSECT连接的结果中,我们从每个CTE中获取所需的值。

最后,Dense_Rank()根据每个群组的标准化时间段为我们提供排名。

答案 2 :(得分:0)

我将以与vkp不同的方式解释问题。另一行8秒内的任何行应该在同一组中。这些值可以链接在一起,因此总跨度可以超过8秒。

这种方法的优点是不需要递归CTE,所以它应该更快。 (当然,如果OP不同意该定义,这不是一个优势。)

基本思路是查看上一个日期/时间值;如果距离超过8秒,则添加一个标志。标志的累积总和是您要查找的行号。

  select t.*,
         sum(case when prev_dt >= dateandtime - interval '8' second
                  then 0 else 1
             end) over (partition by customerid order by dateandtime
                       ) as row_number
  from (select t.*,
               max(dateandtime) over (partition by customerid order by dateandtime row between 1 preceding and 1 preceding) as prev_dt
        from t
       ) t;