we have following table (output is already ordered and separated for understanding):
| PK | FK1 | FK2 | ActionCode | CreationTS | SomeAttributeValue |
+----+-----+-----+--------------+---------------------+--------------------+
| 6 | 100 | 500 | Create | 2011-01-02 00:00:00 | H |
----------------------------------------------------------------------------
| 3 | 100 | 500 | Change | 2011-01-01 02:00:00 | Z |
| 2 | 100 | 500 | Change | 2011-01-01 01:00:00 | X |
| 1 | 100 | 500 | Create | 2011-01-01 00:00:00 | Y |
----------------------------------------------------------------------------
| 4 | 100 | 510 | Create | 2011-01-01 00:30:00 | T |
----------------------------------------------------------------------------
| 5 | 100 | 520 | CreateSystem | 2011-01-01 00:30:00 | A |
----------------------------------------------------------------------------
what is ActionCode? we use this in c# and there it represents an enum-value
what do i want to achieve?
well, i need the following output:
| FK1 | FK2 | ActionCode | SomeAttributeValue |
+-----+-----+--------------+--------------------+
| 100 | 500 | Create | H |
| 100 | 500 | Create | Z |
| 100 | 510 | Create | T |
| 100 | 520 | CreateSystem | A |
-------------------------------------------------
well, what is the actual logic?
we have some logical groups for composite-key (FK1 + FK2). each of these groups can be broken into partitions, which begin with Create or CreateSystem. each partition ends with Create, CreateSystem or Change. The actual value of SomeAttributeValue for each partition should be the value from the last line of the partition.
it is not possible to have following datapool:
| PK | FK1 | FK2 | ActionCode | CreationTS | SomeAttributeValue |
+----+-----+-----+--------------+---------------------+--------------------+
| 7 | 100 | 500 | Change | 2011-01-02 02:00:00 | Z |
| 6 | 100 | 500 | Create | 2011-01-02 00:00:00 | H |
| 2 | 100 | 500 | Change | 2011-01-01 01:00:00 | X |
| 1 | 100 | 500 | Create | 2011-01-01 00:00:00 | Y |
----------------------------------------------------------------------------
and then expect PK 7 to affect PK 2 or PK 6 to affect PK 1.
i don’t even know how/where to start … how can i achieve this?
we are running on mssql 2005+
EDIT:
there’s a dump available:
- instanceId: my PK
- tenantId: FK 1
- campaignId: FK 2
- callId: FK 3
- refillCounter: FK 4
- ticketType: ActionCode (1 & 4 & 6 are
Create, 5 isChange, 3 must be ignored) - ticketType, profileId, contactPersonId, ownerId, handlingStartTime, handlingEndTime, memo, callWasPreselected, creatorId, creationTS, changerId, changeTS should be taken from the
Create(first line in partition in groups) - callingState, reasonId, followUpDate, callingAttempts and callingAttemptsConsecutivelyNotReached should be taken from the last
Create(which then would be a “one-line-partition-in-group” / the same as the upper one) orChange(last line in partition in groups)
I’m assuming that each partition can only contain a single Create or CreateSystem, otherwise your requirements are ill-defined. The following is untested, since I don’t have a sample table, nor sample data in an easily consumed format:
(Please note than I’m using all kinds of reserved names here)
The basic logic is: The Partitions CTE is used to define each partition in terms of the FK1, FK2, an inclusive start timestamp, and exclusive end timestamp. It does this by a triple join to the base table. the rows from
t2are selected to occur after the rows fromt1, then the rows fromt3are selected to occur between the matching rows fromt1andt2. Then, in the WHERE clause, we exclude any rows from the result set where a match occurred fromt3– the result being that the row fromt1and the row fromt2represent the start of two adjacent partitions.The second CTE then retrieves all rows from
Tablefor each partition, but assigning aROW_NUMBER()score within each partition, based on theCreationTS, sorted descending, with the result thatROW_NUMBER()1 within each partition is the last row to occur.Finally, within the select, we choose those rows that occur last within their respective partitions.
This does all assume that
CreationTSvalues are distinct within each partition. I may be able to re-work it using PK also, if that assumption doesn’t hold up.