1

今天是个好日子。我一直无法提出解决方案,正在寻求任何帮助。我有一个表,我想为其添加一列:

ID      Time_Stamp                       Activity_Log
1254    6/1/2013 8:00:00AM         'Ticket was opened by user A'
1254    6/2/2013 8:20:00AM         'Comment was logged by user B'
1254    6/4/2013 8:25:00AM         '**Ticket was transferred to Group 1**'
1254    6/7/2013 8:50:00AM         'Comment was logged by user B'
1254    6/11/2013 10:00:00AM       'Comment was logged by user C'
1254    6/12/2013 12:00:00PM       '**Ticket was transferred to Group 2**'
7589    6/3/2013 11:00:00PM        'Ticket was opened by user A'
7589    6/5/2013 1:00:00PM         'Comment was logged by user C'
7589    6/7/2013 2:00:00PM         'Resolution provided by user C'
7589    6/11/2013 5:00:00PM        'Ticket resolved by user C'
8526    6/3/2013 7:00:00PM        'Ticket opened by user D'
8526    6/4/2013 2:00:00PM        '**Ticket was transferred to Group 2**'
8526    6/5/2013 1:00:00PM        'Comment was logged by user G'
8526    6/8/2013 5:00:00PM        '**Ticket was resolved by user C**'

这是我希望新列 Group 的样子:

ID      Time_Stamp                       Activity_Log                           Group
1254    6/1/2013 8:00:00AM         'Ticket was opened by user A'                NULL
1254    6/2/2013 8:20:00AM         'Comment was logged by user B'               NULL
1254    6/4/2013 8:25:00AM         '**Ticket was transferred to Group 1**'      Group 1
1254    6/7/2013 8:50:00AM         'Comment was logged by user B'               Group 1
1254    6/11/2013 10:00:00AM       'Comment was logged by user C'               Group 1
1254    6/12/2013 12:00:00PM       '**Ticket was transferred to Group 2**'      Group 2
7589    6/3/2013 11:00:00PM        'Ticket was opened by user A'                NULL
7589    6/5/2013 1:00:00PM         'Comment was logged by user C'               NULL
7589    6/7/2013 2:00:00PM         'Resolution provided by user C'              NULL
7589    6/11/2013 5:00:00PM        'Ticket resolved by user C'                  NULL
8526    6/3/2013 7:00:00PM        'Ticket opened by user D'                     NULL
8526    6/4/2013 2:00:00PM        '**Ticket was transferred to Group 2**'       Group 2
8526    6/5/2013 1:00:00PM        'Comment was logged by user G'                Group 2
8526    6/8/2013 5:00:00PM        '**Ticket was resolved by user C**'           Group 2

如果 Activity_Log 是 LIKE '%xx%',我想用一个字符串添加这个组列,并且基于 ID 和 Time_Stamp 之后的每一行。一些 ID 在 activity_log 中没有该语言,并且所有行的 Group 列都为 NULL。对于那些这样做的人,我想在 activity_log 为 LIKE '%transfer to Group x%' 时开始,并在 activity_log 为 '%Transfer to Group x%' 或 '%ticket wassolved%' 时结束。我尝试使用 OVER (partition by) 编写查询以按 ID 划分,但没有成功。

谢谢

4

2 回答 2

2

您可以使用“累积最大值”功能来做到这一点。大多数支持窗口函数的数据库都支持这一点,但 SQL Server 2012 之前的 SQL Server 除外。

这是方法:

select t.id, t.Time_Stamp, t.Activity_Log,
       MAX(GroupName) over (partition by id order by TimeStamp) as "Group"      
from (select t.*,
             (case when Activity_Log like '%transferred to Group%'
                   then left(RIGHT(Activity_Log, 9), 7)
              end) as GroupName
      from t
     ) t;

如果您确实想将其添加为列,则需要更改表以添加列,然后使用update语句。

如果您的数据库不支持累积和,您仍然可以使用相关子查询来完成工作:

select t.id, t.Time_Stamp, t.Activity_Log,
       (select left(RIGHT(t2.Activity_Log, 9), 7)
        from t t2
        where t2.id = t.id and
              t2.TimeStamp <= t.TimeStamp and
              Activity_Log like '%transferred to Group%'
        order by t2.TimeStamp desc
        limit 1
       ) as "Group"
from t;
于 2013-07-23T14:14:43.000 回答
0

我会选择Gordon Linoff's解决方案,但是如果你想存储它,你可以添加你需要的列ALTER TABLE,如果你只想显示信息,那么 aSELECT也可以。

ALTER TABLE tblLog
ADD Group VARCHAR(4)
于 2013-07-23T15:14:50.957 回答