@Fer_Munoz que quiero hacer lo mismo, con la tablas ODPI y ORIN, de hecho la consulta completa es esta
SELECT DISTINCT
–T0.DocEntry,
T0.[DocNum],
T0.[DocDate],
T0.[CreateDate],
T0.[CardCode] AS ‘Código Cliente’,
T0.[CardName] AS ‘Nombre Cliente’,
T0.[NumAtCard],
T0.[ObjType] AS ‘Tipo Factura’,
T0.[DocTotal]- T0.[VatSum] AS ‘Base imponible’,
T0.[VatSum] AS ‘Impuesto’,
T0.[DocTotal] ‘Total Factura’,
T0.[Comments],
T0.[U_NVT_ORDERTYPE],
T3.DocNum AS ‘Nº de Pedido’
FROM [dbo].[OINV]T0
INNER JOIN [dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
LEFT JOIN [dbo].[RDR1] T2 ON T1.BaseEntry = T2.DocEntry AND T1.BaseLine = T2.LineNum AND T1.BaseType = T2.ObjType
LEFT JOIN [dbo].[ORDR] T3 ON T3.DocEntry = T2.DocEntry
WHERE T0.CANCELED = ‘N’
UNION ALL
SELECT DISTINCT
T0.[DocNum],
T0.[DocDate],
T0.[CreateDate],
T0.[CardCode] AS ‘Código Cliente’,
T0.[CardName] AS ‘Nombre Cliente’,
T0.[NumAtCard],
T0.[ObjType] AS ‘Tipo Factura’,
T0.[DocTotal] *-1 - T0.[VatSum] * -1 AS ‘Base imponible’,
T0.[VatSum] * -1 AS ‘Impuesto’,
T0.[DocTotal] * -1 AS ‘Total Factura’,
T0.[Comments],
T0.[U_NVT_ORDERTYPE],
T3.DocNum AS ‘Nº de Pedido’
FROM ORIN T0
INNER JOIN [dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
LEFT JOIN [dbo].[RDR1] T2 ON T1.BaseEntry = T2.DocEntry AND T1.BaseLine = T2.LineNum AND T1.BaseType = T2.ObjType
LEFT JOIN [dbo].[ORDR] T3 ON T3.DocEntry = T2.DocEntry
WHERE T0.[CANCELED] =‘N’
UNION ALL
SELECT DISTINCT
T0.[DocNum],
T0.[DocDate],
T0.[CreateDate],
T0.[CardCode] AS ‘Código Cliente’,
T0.[CardName] AS ‘Nombre Cliente’,
T0.[NumAtCard],
T0.[ObjType] AS ‘Tipo Factura’,
T0.[DocTotal] - T0.[VatSum] AS ‘Base imponible’,
T0.[VatSum] AS ‘Impuesto’,
T0.[DocTotal] AS ‘Total Factura’,
T0.[Comments],
T0.[U_NVT_ORDERTYPE],
T3.DocNum AS ‘Nº de Pedido’
FROM ODPI T0
INNER JOIN [dbo].[INV1] T1 ON T0.DocEntry = T1.DocEntry
LEFT JOIN [dbo].[RDR1] T2 ON T1.BaseEntry = T2.DocEntry AND T1.BaseLine = T2.LineNum AND T1.BaseType = T2.ObjType
LEFT JOIN [dbo].[ORDR] T3 ON T3.DocEntry = T2.DocEntry
WHERE T0.[CANCELED] =‘N’