简体   繁体   English

DB2 SQL-按日期时间排序的两个字段的密集等级分组

[英]DB2 SQL - Dense Rank Grouping of two fields ordered by Datetime

I know this is a problem others must have solved in the past, but in my limited knowledge, have yet to get over the hump. 我知道这是过去其他人必须解决的问题,但据我所知,还没有克服困难。 I have data which is ordered by datetime, which needs to be grouped by a combination of two fields (status and queue). 我有按日期时间排序的数据,需要按两个字段(状态和队列)的组合进行分组。 In instances where the status and queue are the same within a given time frame, they should be considered part of the same group, and thus have the same id. 在给定时间范围内状态和队列相同的情况下,应将它们视为同一组的一部分,因此具有相同的ID。

To accomplish this, I've attempted to implement DENSE_RANK(), and for all intents and purposes, it has been successful - with exception of the ordering of the groups. 为此,我尝试实现DENSE_RANK(),并且出于所有目的和目的,它已经成功完成-除了组的排序。 Below is an example: 下面是一个示例:

WITH TEMP1 (EVENT_DATE, PRV_EVENT_DATE, STATUS, PRV_STATUS, QUEUE, PRV_QUEUE) AS
   (VALUES ('2012-09-04 11:40:19.936141', '', 'CREATED', '', 'SYSTEM', '')
          ,('2012-09-04 11:40:21.207140', '2012-09-04 11:40:19.936141', 'CREATED', 'CREATED', 'SYSTEM', 'SYSTEM')
          ,('2012-09-04 11:40:27.771140', '2012-09-04 11:40:21.207140', 'PROCESS', 'CREATED', 'PROCESS', 'SYSTEM')
          ,('2012-09-05 00:01:20.384180', '2012-09-04 11:40:27.771140', 'SUSPEND', 'PROCESS', 'SYSTEM', 'SYSTEM')
          ,('2012-09-05 00:02:14.042180', '2012-09-05 00:01:20.384180', 'SUSPEND', 'SUSPEND', 'PEND', 'SYSTEM')
          ,('2012-09-06 00:02:14.642180', '2012-09-05 00:02:14.042180', 'SUSPEND', 'SUSPEND', 'SYSTEM', 'SYSTEM')
          ,('2012-09-06 00:02:33.433180', '2012-09-06 00:02:14.642180', 'SUSPEND', 'SUSPEND', 'SYSTEM', 'SYSTEM')
   )  
SELECT 
ROW_NUMBER() OVER (ORDER BY EVENT_DATE) AS "RN",
DENSE_RANK() OVER ( ORDER BY status, queue, date(event_date)) AS "GRP",
EVENT_DATE, PRV_EVENT_DATE, STATUS, PRV_STATUS, QUEUE, PRV_QUEUE
FROM TEMP1
ORDER BY EVENT_DATE

The results are such: 结果是这样的:

RN GRP EVENT_DATE                  PRV_EVENT_DATE             STATUS  PRV_STATUS QUEUE
1   1  2012-09-04 11:40:19.936141                             CREATED            SYSTEM
2   1  2012-09-04 11:40:21.207140  2012-09-04 11:40:19.936141 CREATED CREATED    SYSTEM
3   2  2012-09-04 11:40:27.771140  2012-09-04 11:40:21.207140 PROCESS CREATED    PROCESS 
4   4  2012-09-05 00:01:20.384180  2012-09-04 11:40:27.771140 SUSPEND PROCESS    SYSTEM
5   3  2012-09-05 00:02:14.042180  2012-09-05 00:01:20.384180 SUSPEND SUSPEND    PEND
6   5  2012-09-06 00:02:14.642180  2012-09-05 00:02:14.042180 SUSPEND SUSPEND    SYSTEM

As you can tell, the "GRP" is out of order (and I also know using date(EVENT_DATE) isn't the solution). 如您所知,“ GRP”是乱序的(我也知道使用date(EVENT_DATE)不是解决方案)。

It' not clear (at least to me), what you actually want. (至少对我而言)不清楚您真正想要的是什么。 A new group whenever there's a change of "STATUS" or "QUEUE" compared to the previous one? 每当与之前的状态相比“ STATUS”或“ QUEUE”发生变化时,一个新的组? Or are there more complex rules? 还是有更复杂的规则?

Looks like your data is already the result of a query where you calculate the previous value using a MIN(status/queue) OVER (ROW BETWEEN 1 PRECEDING and 1 PRECEDING) 看起来您的数据已经是查询的结果,其中您使用MIN(status / queue)OVER(1 PRECEDING和1 PRECEDING之间的行)来计算前一个值

You'll never get the right order when you cast to a DATE, try a calculation like this: 当您转换为DATE时,您将永远找不到正确的顺序,请尝试如下计算:

SUM(CASE WHEN status = prv_status AND queue = prv_queue THEN 0 ELSE 1 END)
OVER (ORDER BY event_date 
      ROWS UNBOUNDED PRECEDING)

Edit: Without SUM OVER you have to do use a Scalar Subquery as input to the DENSE_RANK, this should work: 编辑:没有总和,您必须使用标量子查询作为DENSE_RANK的输入,这应该可以工作:

SELECT 
   (SELECT MAX(event_date) 
    FROM TEMP1 AS t2 
    WHERE t2.event_date < t1.event_date
    AND t1.status <> t2.status 
    AND t1.QUEUE <> t2.queue) AS x,

    DENSE_RANK() OVER ( ORDER BY x) AS "GRP",

Of course performance might be horrible. 当然,性能可能会很糟糕。

Maybe you better keep the "wrong" order, at least it's the same wrong value for all rows of a group :-) 也许您最好保持“错误”的顺序,至少对于组中的所有行都使用相同的错误值:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM