I have an sql query that I can not determine,
(
SELECT (
SELECT e.nombre
FROM [Control_Documental].[dbo].[Etapa] AS e
WHERE e.idEtapa = etd.idEtapa
)AS etapa
FROM [Control_Documental].[dbo].[Registro_Etapa_Documento] AS red
INNER JOIN [Control_Documental].[dbo].[Etapa_Tipo_Documento] AS etd ON etd.[idEtapaTipoDocumento] = red.[idEtapaTipoDocumento]
INNER JOIN [Control_Documental].[dbo].[Etapa] AS e ON e.[idEtapa] = etd.[idEtapa]
INNER JOIN [Control_Documental].[dbo].[Registro_Documento] AS rd ON rd.[idRegistroDocumento] = red.[idRegistroDocumento]
INNER JOIN [Control_Documental].[dbo].[Factura] AS fa ON fa.[idFactura] = rd.[idDocumento]
INNER JOIN [Control_Documental].[dbo].[Factura_Orden_Compra] AS fo ON fo.[idFactura] = fa.[idFactura]
INNER JOIN [Control_Documental].[dbo].[Tipo_Documento] AS td ON td.[idTipoDocumento] = rd.[idTipoDocumento]
WHERE CONVERT(CHAR(13), fo.[orden_compra]) = oc.[numero_documento]
AND td.[nombre] = 'Factura'
AND red.recepcionado = 1
AND red.idEtapaTipoDocumento=(select max(red.idEtapaTipoDocumento)
FROM [Control_Documental].[dbo].[Registro_Etapa_Documento] AS red
where red.recepcionado='1' )
)AS estado
my problem is in the 3rd AND
of the 2nd subselect AND red.idEtapaTipoDocumento=
here is where I can not get the status of the stage.
here should bring me the most updated state of the tb_registro_etapa_documento according to the previous query.
but it just brings me the data in NULL.
if to this AND
you will assign a value of the stage, for example:
AND red.idEtapaTipoDocumento='9'
brings me the status for all those who do comply (but it is not the idea). since I need you to bring me the status of the stage for all the data.
Something fails me in this 3rd subselect, if someone could help me I would appreciate it.
I hope you can understand me and help me.