with ENCUESTA as ( select distinct conceptitemid from AFW_SURVEY_user join AFW_SURVEY_ANSWERS on AFW_SURVEY_user.Token = AFW_SURVEY_ANSWERS.Token ) Select * From ( Select --isnull(COUNTRY.description, '-') Pais_del_caso -- , isnull(VENDOR.Company, '-') Compania_del_caso --, isnull(Company.description, '-') Sucursal_del_cliente ASDK_SERVICE_CALL.Serv_id_by_project Id_caso ,'LLAMADA DE SERVICIO' Tipo_de_caso , Afw_status.Stat_name Estado , Usuarios.uname Cliente_Nombre , Usuarios.email Correo , isnull(Usuarios.Phone1, '') + ' ' + isnull(Usuarios.Phone2, '') Telefonos , UsuEspecialista.Uname Especialista , serv_description_nohtml Descripcion_del_caso , ASDK_SERVICE_CALL.Serv_opened_date Fecha_de_registro , ASDK_SERVICE_CALL.Serv_colsed_date Fecha_de_cierre , serv_commentary_nohtml Solucion , ASDK_CATEGORY.CTG_CAPTION Categoria , REGISTRY_TYPE.ret_description Registro , ASDK_REASON.reas_description razon , Case When ENCUESTA.ConceptItemId is null Then 'NO' Else 'SI' End ENCUESTADO , (SELECT setvalue + '/survey/viewer.aspx?token=' + token + '&kinduser=customer' FROM AFW_SURVEY_USER JOIN SETINGENERAL ON setid = 306 where AFW_SURVEY_USER.ConceptItemId = asdk_service_call.serv_id) Link_Encuesta , RT1.ResponseChoiceId Como_calificaria_la_actitud , RT1.ResponseValue Observaciones_Actitud , RT2.ResponseChoiceId Como_calificaria_satisfaccion_sol , RT2.ResponseValue Observacionsol -- , RT3.ResponseChoiceId aprueba_cierre , case when RT3.ResponseChoiceId = 1 then 'SI' when RT3.ResponseChoiceId = 2 then 'NO'else NULL end aprueba_cierre , RT3.ResponseValue observacionesaprueba -- , RT4.ResponseChoiceId Cual_es_su_nivel_satisfaccion_con_respecto_tiempo_de_atencion --, RT4.ResponseValue observacionTiempos /*, (Select top 1 SURV_ANSWER Resp From asdk_Survey_cases Where SURV_QUESTION_ID = 62 and SURV_CASE_ID = ASDK_SERVICE_CALL.Serv_id) Observaciones*/ From ASDK_SERVICE_CALL left outer join Usuarios UsuEspecialista on ASDK_SERVICE_CALL.Serv_responsible_id = UsuEspecialista.Codusuario left outer join Usuarios on ASDK_SERVICE_CALL.Serv_customer_id = Usuarios.Codusuario left outer join Afw_status on ASDK_SERVICE_CALL.Serv_status_id = Afw_status.Stat_id left outer join VENDOR on ASDK_SERVICE_CALL.Serv_vendor_id = VENDOR.Vendorid left outer join Company on Usuarios.Companyid = Company.Companyid left outer join Country on Usuarios.CountryID = Country.countryid inner join ASDK_REASON on ASDK_SERVICE_CALL.serv_reason_id = ASDK_REASON.reas_id inner join ASDK_CATEGORY on ASDK_SERVICE_CALL.serv_category_id = ASDK_CATEGORY.CTG_INDEX inner join REGISTRY_TYPE on ASDK_SERVICE_CALL.serv_registry_type_id = REGISTRY_TYPE.ret_id left join ENCUESTA on ASDK_SERVICE_CALL.serv_id = ENCUESTA.ConceptItemId left join AFW_SURVEY_USER US on US.ConceptItemId = ASDK_SERVICE_CALL.serv_id left join AFW_SURVEY_ANSWERS RT1 on RT1.Token = US.Token and RT1.QuestionId=1 left join AFW_SURVEY_ANSWERS RT2 on RT2.Token = US.Token and RT2.QuestionId=2 left join AFW_SURVEY_ANSWERS RT3 on RT3.Token = US.Token and RT3.QuestionId=3 --left join AFW_SURVEY_ANSWERS RT4 on RT4.Token = US.Token and RT4.QuestionId=4 Where ASDK_SERVICE_CALL.Serv_fl_int_project_id = 1 /*Soporte*/ and ASDK_SERVICE_CALL.Serv_colsed_date between @Fechaini and @Fechafin --and COUNTRY.description in(@Pais) -- and ASDK_SERVICE_CALL.serv_final_status = 0 ) TMP UNION ALL Select * From ( Select -- isnull(COUNTRY.description, '-') Pais_del_caso --, isnull(VENDOR.Company, '-') Compania_del_caso -- , isnull(Company.description, '-') Sucursal_del_cliente ASDK_INCIDENT.INCI_id_by_project Id_caso ,'INCIDENTE' Tipo_de_caso , Afw_status.Stat_name Estado , Usuarios.uname Cliente_Nombre , Usuarios.email Correo , isnull(Usuarios.Phone1, '') + ' ' + isnull(Usuarios.Phone2, '') Telefonos , UsuEspecialista.Uname Especialista , INCI_description_nohtml Descripcion_del_caso , ASDK_INCIDENT.INCI_opened_date Fecha_de_registro , ASDK_INCIDENT.INCI_colsed_date Fecha_de_cierre , INCI_commentary_nohtml Solucion , ASDK_CATEGORY.CTG_CAPTION Categoria , REGISTRY_TYPE.ret_description Registro , ASDK_REASON.reas_description razon , Case When ENCUESTA.ConceptItemId is null Then 'NO' Else 'SI' End ENCUESTADO , (SELECT setvalue + '/survey/viewer.aspx?token=' + token + '&kinduser=customer' FROM AFW_SURVEY_USER JOIN SETINGENERAL ON setid = 306 where AFW_SURVEY_USER.ConceptItemId = asdk_INCIDENT.INCI_id) Link_Encuesta , RT1.ResponseChoiceId Como_calificaria_la_actitud , RT1.ResponseValue Observaciones_Actitud , RT2.ResponseChoiceId Como_calificaria_satisfaccion_sol , RT2.ResponseValue Observacionsol -- , RT3.ResponseChoiceId aprueba_cierre , case when RT3.ResponseChoiceId = 1 then 'SI' when RT3.ResponseChoiceId = 2 then 'NO'else NULL end aprueba_cierre , RT3.ResponseValue observacionesaprueba -- , RT4.ResponseChoiceId Cual_es_su_nivel_satisfaccion_con_respecto_tiempo_de_atencion -- , RT4.ResponseValue obINCIacionTiempos /*, (Select top 1 SURV_ANSWER Resp From asdk_Survey_cases Where SURV_QUESTION_ID = 62 and SURV_CASE_ID = ASDK_INCIDENT.INCI_id) ObINCIaciones*/ From ASDK_INCIDENT left outer join Usuarios UsuEspecialista on ASDK_INCIDENT.INCI_responsible_id = UsuEspecialista.Codusuario left outer join Usuarios on ASDK_INCIDENT.INCI_customer_id = Usuarios.Codusuario left outer join Afw_status on ASDK_INCIDENT.INCI_status_id = Afw_status.Stat_id left outer join VENDOR on ASDK_INCIDENT.INCI_vendor_id = VENDOR.Vendorid left outer join Company on Usuarios.Companyid = Company.Companyid left outer join Country on Usuarios.CountryID = Country.countryid inner join ASDK_REASON on ASDK_INCIDENT.INCI_reason_id = ASDK_REASON.reas_id inner join ASDK_CATEGORY on ASDK_INCIDENT.INCI_category_id = ASDK_CATEGORY.CTG_INDEX inner join REGISTRY_TYPE on ASDK_INCIDENT.INCI_registry_type_id = REGISTRY_TYPE.ret_id left join ENCUESTA on ASDK_INCIDENT.INCI_id = ENCUESTA.ConceptItemId left join AFW_SURVEY_USER US on US.ConceptItemId = ASDK_INCIDENT.INCI_id left join AFW_SURVEY_ANSWERS RT1 on RT1.Token = US.Token and RT1.QuestionId=1 left join AFW_SURVEY_ANSWERS RT2 on RT2.Token = US.Token and RT2.QuestionId=2 left join AFW_SURVEY_ANSWERS RT3 on RT3.Token = US.Token and RT3.QuestionId=3 -- left join AFW_SURVEY_ANSWERS RT4 on RT4.Token = US.Token and RT4.QuestionId=4 Where ASDK_INCIDENT.INCI_fl_int_project_id = 1 /*Soporte*/ and ASDK_INCIDENT.INCI_colsed_date between @Fechaini and @Fechafin -- and COUNTRY.description in(@Pais) --and ASDK_INCIDENT.INCI_final_status = 0 ) TMP2 Where isnull(Link_Encuesta, '') <> '' Order by Id_caso