Hi All,
I need some help with this one, perhaps I am looking at it the wrong way around - any suggestions would be appreciated.
Below is what my data looks like:
CAL_DATE | INTERACTION_ID | INTERACTION_RESOURCE_ID | QUEUE_NAME |
---|
2015-07-14 09:51:10 | 4358118 | 169758170 | NULL |
2015-07-14 09:51:11 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB |
2015-07-14 11:43:17 | 4358118 | 169813426 | HMB_SitelCCO_WB |
2015-07-14 11:56:06 | 4358118 | 169818626 | desktop_in_progress |
2015-07-14 12:14:07 | 4358118 | 169826178 | desktop_in_progress |
2015-07-14 12:44:04 | 4358118 | 169838050 | desktop_in_progress |
2015-07-14 19:01:32 | 4358118 | 170005574 | desktop_in_progress |
2015-07-14 19:04:57 | 4358118 | 170005830 | desktop_in_progress |
2015-07-14 19:04:57 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB |
2015-07-15 15:27:04 | 4358118 | 170256386 | HMB_SitelCCO_Escalations_WB |
2015-07-15 15:27:05 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB |
2015-07-15 19:24:54 | 4358118 | 170329642 | HMB_SitelCCO_WB |
2015-07-15 19:31:54 | 4358118 | 170330926 | desktop_in_progress |
2015-07-15 19:31:54 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB |
When the queue name is in a predefined list, I need to find the most recent queue.
So I have a statement that establishes if it is in that predefined list or not:
case when INSTR(UPPER(queue_name), UPPER('desktop_in_progress')) = 0
and INSTR(UPPER(queue_name), UPPER('Forward e-mails')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('NONE')) = 0
and INSTR(UPPER(queue_name), UPPER('Termination failure')) = 0
and INSTR(UPPER(queue_name), UPPER('internetsales_q')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_in_progress/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('inbound_email_q')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('Workbin for Original Agent')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft')) = 0
and INSTR(UPPER(queue_name), UPPER('specialdeals_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Inbound queue')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop-draft-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound e-mails1')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound_email_q')) = 0
then ROW_NUMBER() over (partition by interaction_id order by cal_date desc)
The output of this is then:
CAL_DATE | INTERACTION_ID | INTERACTION_RESOURCE_ID | QUEUE_NAME | ROWNUM_ |
---|
2015-07-14 09:51:10 | 4358118 | 169758170 | NULL | NULL |
2015-07-14 09:51:11 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB | 16 |
2015-07-14 11:43:17 | 4358118 | 169813426 | HMB_SitelCCO_WB | 15 |
2015-07-14 11:56:06 | 4358118 | 169818626 | desktop_in_progress | NULL |
2015-07-14 12:14:07 | 4358118 | 169826178 | desktop_in_progress | NULL |
2015-07-14 12:44:04 | 4358118 | 169838050 | desktop_in_progress | NULL |
2015-07-14 19:01:32 | 4358118 | 170005574 | desktop_in_progress | NULL |
2015-07-14 19:04:57 | 4358118 | 170005830 | desktop_in_progress | NULL |
2015-07-14 19:04:57 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB | 10 |
2015-07-15 15:27:04 | 4358118 | 170256386 | HMB_SitelCCO_Escalations_WB | 8 |
2015-07-15 15:27:05 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB | 7 |
2015-07-15 19:24:54 | 4358118 | 170329642 | HMB_SitelCCO_WB | 6 |
2015-07-15 19:31:54 | 4358118 | 170330926 | desktop_in_progress | NULL |
2015-07-15 19:31:54 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB | 4 |
So what essentially what I am trying to do is find the LEAD QUEUE_NAME where the ROWNUM_ is not null, but I am having issues where there are multiple instances in a row of queue names in my predefined list - see the multiple instances of desktop_in_progress above.
Below is the query I wrote:
select cal_date,
interaction_id,
interaction_resource_id,
queue_name,
case when INSTR(UPPER(queue_name), UPPER('desktop_in_progress')) = 0
and INSTR(UPPER(queue_name), UPPER('Forward e-mails')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('NONE')) = 0
and INSTR(UPPER(queue_name), UPPER('Termination failure')) = 0
and INSTR(UPPER(queue_name), UPPER('internetsales_q')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_in_progress/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('inbound_email_q')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('Workbin for Original Agent')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft')) = 0
and INSTR(UPPER(queue_name), UPPER('specialdeals_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Inbound queue')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop-draft-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound e-mails1')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound_email_q')) = 0
then ROW_NUMBER() over (partition by interaction_id order by cal_date desc) end as rownum_,
case when(
case when INSTR(UPPER(queue_name), UPPER('desktop_in_progress')) = 0
and INSTR(UPPER(queue_name), UPPER('Forward e-mails')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('NONE')) = 0
and INSTR(UPPER(queue_name), UPPER('Termination failure')) = 0
and INSTR(UPPER(queue_name), UPPER('internetsales_q')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_in_progress/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('inbound_email_q')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip-workbin/PrivateQueue')) = 0
and INSTR(UPPER(queue_name), UPPER('Workbin for Original Agent')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop_draft')) = 0
and INSTR(UPPER(queue_name), UPPER('specialdeals_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Inbound queue')) = 0
and INSTR(UPPER(queue_name), UPPER('desktop-draft-workbin')) = 0
and INSTR(UPPER(queue_name), UPPER('staticip_q')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound e-mails1')) = 0
and INSTR(UPPER(queue_name), UPPER('Outbound_email_q')) = 0
then ROW_NUMBER() over (partition by interaction_id order by cal_date desc)
end) is null then LEAD(queue_name) over (partition by interaction_id order by cal_date desc) else queue_name end as lead_queue
from s_inf_voice_interactions_key
where interaction_id = 4358118
order by cal_date
Which gives the following output
CAL_DATE | INTERACTION_ID | INTERACTION_RESOURCE_ID | QUEUE_NAME | ROWNUM_ | LEAD_QUEUE |
---|
2015-07-14 09:51:10 | 4358118 | 169758170 | NULL | NULL | NULL |
2015-07-14 09:51:11 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB | 16 | WB_TFR_HMB_SITEL_CCO_WB |
2015-07-14 11:43:17 | 4358118 | 169813426 | HMB_SitelCCO_WB | 15 | HMB_SitelCCO_WB |
2015-07-14 11:56:06 | 4358118 | 169818626 | desktop_in_progress | NULL | HMB_SitelCCO_WB |
2015-07-14 12:14:07 | 4358118 | 169826178 | desktop_in_progress | NULL | desktop_in_progress |
2015-07-14 12:44:04 | 4358118 | 169838050 | desktop_in_progress | NULL | desktop_in_progress |
2015-07-14 19:01:32 | 4358118 | 170005574 | desktop_in_progress | NULL | desktop_in_progress |
2015-07-14 19:04:57 | 4358118 | 170005830 | desktop_in_progress | NULL | desktop_in_progress |
2015-07-14 19:04:57 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB | 10 | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB |
2015-07-15 15:27:04 | 4358118 | 170256386 | HMB_SitelCCO_Escalations_WB | 8 | HMB_SitelCCO_Escalations_WB |
2015-07-15 15:27:05 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_WB | 7 | WB_TFR_HMB_SITEL_CCO_WB |
2015-07-15 19:24:54 | 4358118 | 170329642 | HMB_SitelCCO_WB | 6 | HMB_SitelCCO_WB |
2015-07-15 19:31:54 | 4358118 | 170330926 | desktop_in_progress | NULL | HMB_SitelCCO_WB |
2015-07-15 19:31:54 | 4358118 | NULL | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB | 4 | WB_TFR_HMB_SITEL_CCO_ESCALATIONS_WB |
So it works for the leading value, but not in examples where I have the predefined values in a list, obviously as that's how LEAD works.
So what I think I need to do is some sort of LEAD(LOOP QUEUE_NAME where ROWNUM_ IS NOT NULL) ??
Not sure..
Any help appreciated.
Thanks!