如何消除我不想要的表中的行

时间:2016-11-24 09:45:24

标签: sql sql-server sql-server-2012

这是我的表。我希望特定行的手机号码只有一个升级呼叫记录和近距离呼叫

Mobile_No   Call_Time           Pick_Time           Close_Time        Call_Duration   CallStatus
7777777777  23-11-2016 16:47    23-11-2016 16:47    NULL              NULL             ATTEND
8080808080  21-11-2016 12:59    21-11-2016 13:02    21-11-2016 13:03  0.001446759      ESCALATE
8080808080  21-11-2016 12:59    21-11-2016 13:03    NULL              NULL             ESCALATE
8087617483  21-11-2016 13:05    23-11-2016 16:02    23-11-2016 16:02  00:00:18         CLOSE
8087617483  21-11-2016 13:05    23-11-2016 16:02    NULL              NULL             CLOSE
8655246108  21-11-2016 13:05    22-11-2016 10:59    23-11-2016 16:39  30:1780:106836   CLOSE
8655246108  21-11-2016 13:05    23-11-2016 16:39    24-11-2016 11:44  19:1145:68698    CLOSE
9167485255  21-11-2016 13:07    23-11-2016 16:34    23-11-2016 16:38  0.005659722      ESCALATE
9167485255  21-11-2016 13:07    23-11-2016 16:38    NULL              NULL             ESCALATE
9890473692  21-11-2016 13:07    22-11-2016 11:36    22-11-2016 11:53  0.023622685      CLOSE
9890473692  21-11-2016 13:07    22-11-2016 11:53    24-11-2016 11:31  48:2858:171423   CLOSE
9890473692  24-11-2016 11:29    24-11-2016 11:31    24-11-2016 11:47  0.022337963      ATTEND

现在我希望输出为

Mobile_No   Call_Time           Pick_Time              Close_Time           Call_Duration    CallStatus
7777777777  2016-11-23 16:47:26 2016-11-23 16:47:43    NULL                 NULL             ATTEND
8080808080  2016-11-21 12:59:44 2016-11-21 13:02:15    2016-11-21 13:03:20  0:1:65           ESCALATE
8080808080  2016-11-21 12:59:44 2016-11-21 13:03:20    NULL                 NULL             ESCALATE
8087617483  2016-11-21 13:05:04 2016-11-23 16:02:35    2016-11-23 16:02:53  0:0:18           CLOSE
8087617483  2016-11-21 13:05:04 2016-11-23 16:02:53    NULL                 NULL             CLOSE
8655246108  2016-11-21 13:05:59 2016-11-22 10:59:16    2016-11-23 16:39:52  30:1780:106836   CLOSE 
8655246108  2016-11-21 13:05:59 2016-11-23 16:39:52    2016-11-24 11:44:50  19:1145:68698    CLOSE
9167485255  2016-11-21 13:07:57 2016-11-23 16:34:16    2016-11-23 16:38:25  0:4:249 ESCALATE
9167485255  2016-11-21 13:07:57 2016-11-23 16:38:25     NULL                NULL             ESCALATE
9890473692  2016-11-21 13:07:00 2016-11-22 11:36:56    2016-11-22 11:53:57  0:17:1021         CLOSE
9890473692  2016-11-21 13:07:00 2016-11-22 11:53:57    2016-11-24 11:31:00  48:2858:171423  CLOSE
9890473692  2016-11-24 11:29:20 2016-11-24 11:31:00     2016-11-24 11:47:10 0:16:970         ATTEND

3 个答案:

答案 0 :(得分:0)

尝试此查询

select Mobile_No,to_char(Call_Time,'yyyy-mm-dd hh12:mm:ss'),Pick_Time,Close_Time,Call_Duration,CallStatus from table where Mobile_No in
(select Mobile_No  from table having (select count(*) from table where callstatus ='escalate')=1 and having (select count(*) from table where callstatus ='close')=1 )

答案 1 :(得分:0)

        CREATE TABLE Table11
    (Mobile_No bigint, Call_Time varchar(16), Pick_Time varchar(16), Close_Time varchar(16), Call_Duration varchar(14), CallStatus varchar(8))
;

INSERT INTO Table11 (Mobile_No, Call_Time, Pick_Time, Close_Time, Call_Duration, CallStatus) VALUES
    (7777777777, '23-11-2016 16:47', '23-11-2016 16:47', NULL, NULL, 'ATTEND'),
    (8080808080, '21-11-2016 12:59', '21-11-2016 13:02', '21-11-2016 13:03', '0.001446759', 'ESCALATE'),
    (8080808080, '21-11-2016 12:59', '21-11-2016 13:03', NULL, NULL, 'ESCALATE'),
    (8087617483, '21-11-2016 13:05', '23-11-2016 16:02', '23-11-2016 16:02', '00:00:18', 'CLOSE'),
    (8087617483, '21-11-2016 13:05', '23-11-2016 16:02', NULL, NULL, 'CLOSE'),
    (8655246108, '21-11-2016 13:05', '22-11-2016 10:59', '23-11-2016 16:39', '30:1780:106836', 'CLOSE'),
    (8655246108, '21-11-2016 13:05', '23-11-2016 16:39', '24-11-2016 11:44', '19:1145:68698', 'CLOSE'),
    (9167485255, '21-11-2016 13:07', '23-11-2016 16:34', '23-11-2016 16:38', '0.005659722', 'ESCALATE'),
    (9167485255, '21-11-2016 13:07', '23-11-2016 16:38', NULL, NULL, 'ESCALATE'),
    (9890473692, '21-11-2016 13:07', '22-11-2016 11:36', '22-11-2016 11:53', '0.023622685', 'CLOSE'),
    (9890473692, '21-11-2016 13:07', '22-11-2016 11:53', '24-11-2016 11:31', '48:2858:171423', 'CLOSE'),
    (9890473692, '24-11-2016 11:29', '24-11-2016 11:31', '24-11-2016 11:47', '0.022337963', 'ATTEND')
;
with cte as
(
select * ,row_number() over (partition by mobile_no,CallStatus order by call_time) as rn from Table11
)
select * from cte where rn=1 and CallStatus not in('ESCALATE','CLOSE')

输出

Mobile_No   Call_Time   Pick_Time   Close_Time  Call_Duration   CallStatus  rn
8080808080  21-11-2016 12:59    21-11-2016 13:02    21-11-2016 13:03    0.001446759 ESCALATE    1
8087617483  21-11-2016 13:05    23-11-2016 16:02    23-11-2016 16:02    00:00:18    CLOSE   1
8655246108  21-11-2016 13:05    22-11-2016 10:59    23-11-2016 16:39    30:1780:106836  CLOSE   1
9167485255  21-11-2016 13:07    23-11-2016 16:34    23-11-2016 16:38    0.005659722 ESCALATE    1
9890473692  21-11-2016 13:07    22-11-2016 11:36    22-11-2016 11:53    0.023622685 CLOSE   1

答案 2 :(得分:0)

试试这个

WITH cte as
(
    SELECT Mobile_No, Call_Time, Pick_Time, Close_Time, Call_Duration, CallStatus,
            ROW_NUMBER() OVER (PARTITION  BY Mobile_No,CallStatus ORDER     BY Call_Time) AS ROWNUM 
    FROM TableName
)
SELECT Mobile_No, Call_Time, Pick_Time, Close_Time, Call_Duration, CallStatus 
FROM cte
WHERE ROWNUM=1 and CallStatus <>'ATTEND'