on 08-21-2015 3:17 PM
Estimados.
Estoy haciendo una query para los acuerdos globales y tengo un problema y que además es un problema bien recurrente. Por favor alguien me puede explicar la solución?
Necesito agregar la fecha de la última Orden de Venta asociada al Acuerdo pero cuando lo hago se me duplican las filas tantas veces como ordenes de ventas hay asociadas al acuerdo global.
esta es la Query:
SELECT distinct T0.AbsID 'Nro AG', t1.freetxt, T0.BpCode 'Rut', T0.BpName 'Nombre', t3.slpname, T0.StartDate, T0.EndDate, T1.Itemname, T1.PlanQty, T1.UnitPrice 'Precio', t1.cumQty, t1.cumamntlc 'Importe Acumulado', (t1.planQty * t1.unitprice) - t1.cumamntlc 'Importe Pendiente'
FROM OOAT T0 INNER JOIN OAT1 T1 ON T0.AbsID = T1.AgrNo inner join OCRD t2 on t0.bpcode = t2.cardcode inner Join OSLP t3 on t2.slpcode = t3.slpcode left join RDR1 t4 on t0.absid = t4.agrno
WHERE T0.Cancelled = 'N' And T0.TermDate IS NULL
¿Como hago para que solo que devuelva una linea?
hasta aquí todo ok:
Pero agrego el campo t4.docdate y me aparece todo duplicado:
Saludos a todos.
El problema que tienes es porque el join está hacia la tabla de detalle de la orden de venta, ¿porqué no mejor buscas en la ORDR la fecha?
algo así
SELECT distinct T0.AbsID 'Nro AG', t1.freetxt, T0.BpCode 'Rut', T0.BpName 'Nombre', t3.slpname, T0.StartDate, T0.EndDate, T1.Itemname, T1.PlanQty, T1.UnitPrice 'Precio', t1.cumQty, t1.cumamntlc 'Importe Acumulado', (t1.planQty * t1.unitprice) - t1.cumamntlc 'Importe Pendiente'
,X.docdate
FROM OOAT T0 INNER JOIN OAT1 T1 ON T0.AbsID = T1.AgrNo inner join OCRD t2 on t0.bpcode = t2.cardcode inner Join OSLP t3 on t2.slpcode = t3.slpcode left join RDR1 t4 on t0.absid = t4.agrno
join ordr x on t4.docentry=x.docentry
WHERE T0.Cancelled = 'N' And T0.TermDate IS NULL
a ver si te funciona
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No me funciona, llego a lo mismo. básicamente independiente de si busco en la ORDR o en la RDR1 se duplican las lineas de acuerdos globales por que hay varias ordenes de ventas asociadas. Lo que yo necesito es que me devuelva solamente la fecha de la última orden de venta relacionada al Acuerdo Global.
Hola Jens.
Prueba con esto:
SELECT distinct T0.AbsID 'Nro AG', t1.freetxt, T0.BpCode 'Rut', T0.BpName 'Nombre', t3.slpname, T0.StartDate, T0.EndDate, T1.Itemname, T1.PlanQty, T1.UnitPrice 'Precio', t1.cumQty, t1.cumamntlc 'Importe Acumulado', (t1.planQty * t1.unitprice) - t1.cumamntlc 'Importe Pendiente',
(Select MAX(T4.DocDate) as Fecha FROM RDR1 T4 WHERE T0.Absid = T4.agrno)
FROM OOAT T0 INNER JOIN OAT1 T1 ON T0.AbsID = T1.AgrNo inner join OCRD t2 on t0.bpcode = t2.cardcode inner Join OSLP t3 on t2.slpcode = t3.slpcode
WHERE T0.Cancelled = 'N' And T0.TermDate IS NULL
Lo que se ha agregado es una subconsulta que trae la fecha más reciente de la orden de compra asociada (Resaltado en negritas)
Prueba y nos comentas.
User | Count |
---|---|
104 | |
12 | |
11 | |
6 | |
5 | |
4 | |
3 | |
3 | |
3 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.