I would like to know if you can join three tables but with different conditions, one winged result of the other.
---- First Consultation ----
--Atenciones Emergencia
select
year(fecha)*100+month(fecha) as Periodo,
NombreMedicoAtencion as Medico ,
TipoAtencionNombre,
count(distinct CodigoOA) as Atenc_Emer
from TBL_QV_Hecho_Produccion_IF
where
year(fecha)>=2018
and CodigoSucursal='ceg'
and GrupoProduccion='Consultas'
and IdTipoAtencion=2
group by year(fecha)*100+month(fecha), NombreMedicoAtencion ,TipoAtencionNombre
order by Medico, Periodo
---- Second Consultation ----
--Consultorio Externo
select
year(fecha)*100+month(fecha) as Periodo,
NombreMedicoAtencion as Medico,
TipoAtencionNombre,
sum(cantidad) as Atenc_Amb from TBL_QV_Hecho_Produccion_IF
where
year(fecha)>=2016
and CodigoSucursal='ceg'
and GrupoProduccion='Consultas'
and IdTipoAtencion=1
group by year(fecha)*100+month(fecha),NombreMedicoAtencion, TipoAtencionNombre
order by Medico, Periodo
---- Third Consultation ----
----Egresos hospitalario
select
year(fecha)*100+month(fecha) as Periodo,
IdMedicoAtencion ,
NombreMedicoAtencion as Medico,
count(distinct CodigoOA) as Egresos
from TBL_QV_Hecho_Produccion_IF
where
year(fecha)>=2016
and CodigoSucursal='ceg'
and IdTipoAtencion=3
and NombreEstadoDocumento<>'anulado'
and GrupoProduccion='hospitalizacion'
group by year(fecha)*100+month(fecha), NombreMedicoAtencion
order by Medico, Periodo
As you can see there are three queries with different conditions, what I was trying to do was to have
Only the headers
PERIOD DOCTOR COUNT (Distinct CodigoOA) ---- > of the first consultation Sum (Quantities) ----- > of the second consultation COUNT (Distinct CodeTo) ----- > of the third consultation
the goal is to have all the doctors but for amounts of their services.