DECLARE @t TABLE(id int, value varchar(10))
INSERT @t SELECT 1, 'aa'
UNION ALL SELECT 1, 'bb'
UNION ALL SELECT 2, 'aaa'
UNION ALL SELECT 2, 'bbb'
UNION ALL SELECT 2, 'ccc'-- 查询处理
SELECT *
FROM(
    SELECT DISTINCT 
        id
    FROM @t
)A
OUTER APPLY(
    SELECT 
        [values]= STUFF(REPLACE(REPLACE(
            (
                SELECT value FROM @t N
                WHERE id = A.id
                FOR XML AUTO
            ), '<N value="', ','), '"/>', ''), 1, 1, '')
)N

解决方案 »

  1.   


    DECLARE @t TABLE(id int, value varchar(10))
    INSERT @t SELECT 1, 'aa'
    UNION ALL SELECT 1, 'bb'
    UNION ALL SELECT 2, 'aaa'
    UNION ALL SELECT 2, 'bbb'
    UNION ALL SELECT 2, 'ccc'-- 查询处理
    SELECT *
    FROM(
        SELECT DISTINCT 
            id
        FROM @t
    )A
    OUTER APPLY(
        SELECT 
            [values]= STUFF(REPLACE(REPLACE(
                (
                    SELECT value FROM @t N
                    WHERE id = A.id
                    FOR XML AUTO
                ), '<N value="', ','), '"/>', ''), 1, 1, '')
    )N
      

  2.   

    select * from 对话记录 a where exists(select 1 from 对话记录 where 客服=a.客服 and 访客<> a.访客 and (
    (开始时间>a.开始时间 and 结束时间<a.开始时间) or(结束时间<a.开始时间 and 结束时间>a.结束时间)
    )
      

  3.   

    create table 对话记录(客服 nvarchar(10),访客 varchar(30),请求类型 nvarchar(10),开始时间 datetime,结束时间 datetime,对话时长 varchar(10),对话ID int)
    insert into 对话记录 select '在线客服1','SNC00000000000012998274','访客请求','2009-06-30 08:45:52','2009-06-30 08:49:00','00: 03: 08',1 
    insert into 对话记录 select '在线客服1','SNC00000000000012998527','访客请求','2009-06-30 08:54:03','2009-06-30 08:54:57','00: 00: 54',2 
    insert into 对话记录 select '在线客服1','SNC00000000000013000662','访客请求','2009-06-30 10:18:13','2009-06-30 10:18:31','00: 00: 18',4 
    insert into 对话记录 select '在线客服1','SNC00000000000006146085','访客请求','2009-06-30 10:49:08','2009-06-30 10:54:14','00: 05: 06',6 
    insert into 对话记录 select '在线客服1','SNC00000000000013002019','访客请求','2009-06-30 10:55:11','2009-06-30 11:21:52','00: 26: 41',9 
    insert into 对话记录 select '在线客服1','SNC00000000000012999410','访客请求','2009-06-30 11:05:08','2009-06-30 11:06:03','00: 00: 55',11 
    insert into 对话记录 select '在线客服1','SNC00000000000013003251','访客请求','2009-06-30 11:32:38','2009-06-30 11:32:45','00: 00: 07',13 
    insert into 对话记录 select '在线客服1','SNC00000000000013003411','访客请求','2009-06-30 11:36:57','2009-06-30 11:39:57','00: 03: 00',15 
    insert into 对话记录 select '在线客服1','SNC00000000000012755658','访客请求','2009-06-30 11:44:46','2009-06-30 11:59:52','00: 15: 06',17 
    insert into 对话记录 select '在线客服1','SNC00000000000013004778','访客请求','2009-06-30 12:20:36','2009-06-30 12:24:17','00: 03: 41',19 
    insert into 对话记录 select '在线客服1','SNC00000000000013005541','访客请求','2009-06-30 12:46:57','2009-06-30 12:52:51','00: 05: 54',20 
    go
    select * from 对话记录 a where exists(select 1 from 对话记录 where 客服=a.客服 and 访客<> a.访客 and (
    (开始时间<a.开始时间 and 结束时间>a.开始时间) or(开始时间<a.结束时间 and 结束时间>a.结束时间) or(开始时间>a.开始时间 and 结束时间<a.结束时间)
    ))
    go
    drop table 对话记录
    /*
    客服         访客                             请求类型       开始时间                    结束时间                    对话时长       对话ID
    ---------- ------------------------------ ---------- ----------------------- ----------------------- ---------- -----------
    在线客服1      SNC00000000000013002019        访客请求       2009-06-30 10:55:11.000 2009-06-30 11:21:52.000 00: 26: 41 9
    在线客服1      SNC00000000000012999410        访客请求       2009-06-30 11:05:08.000 2009-06-30 11:06:03.000 00: 00: 55 11*/
      

  4.   

    create table 对话记录(客服 nvarchar(10),访客 varchar(30),请求类型 nvarchar(10),开始时间 datetime,结束时间 datetime,对话时长 varchar(10),对话ID int)
    insert into 对话记录 select '在线客服1','SNC00000000000012998274','访客请求','2009-06-30 08:45:52','2009-06-30 08:49:00','00: 03: 08',1 
    insert into 对话记录 select '在线客服1','SNC00000000000012998527','访客请求','2009-06-30 08:54:03','2009-06-30 08:54:57','00: 00: 54',2 
    insert into 对话记录 select '在线客服1','SNC00000000000013000662','访客请求','2009-06-30 10:18:13','2009-06-30 10:18:31','00: 00: 18',4 
    insert into 对话记录 select '在线客服1','SNC00000000000006146085','访客请求','2009-06-30 10:49:08','2009-06-30 10:54:14','00: 05: 06',6 
    insert into 对话记录 select '在线客服1','SNC00000000000013002019','访客请求','2009-06-30 10:55:11','2009-06-30 11:21:52','00: 26: 41',9 
    insert into 对话记录 select '在线客服1','SNC00000000000012999410','访客请求','2009-06-30 11:05:08','2009-06-30 11:06:03','00: 00: 55',11 
    insert into 对话记录 select '在线客服1','SNC00000000000013003251','访客请求','2009-06-30 11:32:38','2009-06-30 11:32:45','00: 00: 07',13 
    insert into 对话记录 select '在线客服1','SNC00000000000013003411','访客请求','2009-06-30 11:36:57','2009-06-30 11:39:57','00: 03: 00',15 
    insert into 对话记录 select '在线客服1','SNC00000000000012755658','访客请求','2009-06-30 11:44:46','2009-06-30 11:59:52','00: 15: 06',17 
    insert into 对话记录 select '在线客服1','SNC00000000000013004778','访客请求','2009-06-30 12:20:36','2009-06-30 12:24:17','00: 03: 41',19 
    insert into 对话记录 select '在线客服1','SNC00000000000013005541','访客请求','2009-06-30 12:46:57','2009-06-30 12:52:51','00: 05: 54',20 
    go
    select 客服,开始时间,结束时间,
    对话id=convert(varchar,(select max(对话ID) from 对话记录 where 对话ID<t.对话ID))+','+convert(varchar,对话id)
    from (
    select * from 对话记录 a where 开始时间<(select top 1 结束时间 from 对话记录 where 对话ID<a.对话ID order by  对话ID desc)
    and 开始时间>(select top 1 开始时间 from 对话记录 where 对话ID<a.对话ID order by  对话ID desc)) t/*
    在线客服1 2009-06-30 11:05:08.000 2009-06-30 11:06:03.000 9,11
    */
      

  5.   


    create table 对话记录(客服 nvarchar(10),访客 varchar(30),请求类型 nvarchar(10),开始时间 datetime,结束时间 datetime,对话时长 varchar(10),对话ID int)
    insert into 对话记录 select '在线客服1','SNC00000000000012998274','访客请求','2009-06-30 08:45:52','2009-06-30 08:49:00','00: 03: 08',1 
    insert into 对话记录 select '在线客服1','SNC00000000000012998527','访客请求','2009-06-30 08:54:03','2009-06-30 08:54:57','00: 00: 54',2 
    insert into 对话记录 select '在线客服1','SNC00000000000013000662','访客请求','2009-06-30 10:18:13','2009-06-30 10:18:31','00: 00: 18',4 
    insert into 对话记录 select '在线客服1','SNC00000000000006146085','访客请求','2009-06-30 10:49:08','2009-06-30 11:20:14','00: 05: 06',6 
    insert into 对话记录 select '在线客服1','SNC00000000000013002019','访客请求','2009-06-30 10:55:11','2009-06-30 11:21:52','00: 26: 41',9 
    insert into 对话记录 select '在线客服1','SNC00000000000012999410','访客请求','2009-06-30 11:05:08','2009-06-30 11:06:03','00: 00: 55',11 
    insert into 对话记录 select '在线客服1','SNC00000000000013003251','访客请求','2009-06-30 11:32:38','2009-06-30 11:32:45','00: 00: 07',13 
    insert into 对话记录 select '在线客服1','SNC00000000000013003411','访客请求','2009-06-30 11:36:57','2009-06-30 11:39:57','00: 03: 00',15 
    insert into 对话记录 select '在线客服1','SNC00000000000012755658','访客请求','2009-06-30 11:44:46','2009-06-30 11:59:52','00: 15: 06',17 
    insert into 对话记录 select '在线客服1','SNC00000000000013004778','访客请求','2009-06-30 12:20:36','2009-06-30 12:24:17','00: 03: 41',19 
    insert into 对话记录 select '在线客服1','SNC00000000000013005541','访客请求','2009-06-30 12:46:57','2009-06-30 12:52:51','00: 05: 54',20 
    go
    select 客服,开始时间,结束时间,
    对话id=convert(varchar,(select max(对话ID) from 对话记录 where 对话ID<t.对话ID))+','+convert(varchar,对话id)
    from (
    select * from 对话记录 a where 开始时间<(select top 1 结束时间 from 对话记录 where 对话ID<a.对话ID order by  对话ID desc)
    and 开始时间>(select top 1 开始时间 from 对话记录 where 对话ID<a.对话ID order by  对话ID desc)) t
    /*
    在线客服1 2009-06-30 10:55:11.000 2009-06-30 11:21:52.000 6,9
    在线客服1 2009-06-30 11:05:08.000 2009-06-30 11:06:03.000 9,11*/