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.
Hola
Prueba 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',
T5.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
left join ORDR T5 on T5.DocEntry=T4.DocEntry
WHERE T0.Cancelled = 'N' And T0.TermDate IS NULL
Saludos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Tampoco me resulta.
Miren yo creo que este es un buen ejemplo:
La oportunidad '1712' tiene varias etapas (3) ...
La siguiente Query devuelve solamente la última etapa en uno de sus campos sin triplicarse las filas:
El tema es que no entiendo que es lo que se hizo en esta Query para replicarlo a las query que yo necesito.
Esta es la Query (disculpen lo desordenada pero no se adjuntarla como la de Felipe)
SELECT T0.OpprId 'Numero de oportunidad', T0.Name 'Nombre Oportunidad', t0.cardcode 'Rut', T0.CardName 'Cliente', T1.SlpName 'Vendedor', CASE T0.intRate when '1' then 'Verde' when '2' then 'Amarillo' when '3' then 'Rojo' END 'Avance', T0.OpenDate 'Fecha de inicio', T0.CloseDate 'Fecha de cierre', T0.PredDate 'Fecha de cierre prevista', T0.MaxSumLoc 'Monto Potencial', T0.Cloprcnt '% de cierre', CONVERT(INT,T0.MaxSumLoc*T0.Cloprcnt/100) 'Importe Ponderado', CASE T0.Status WHEN 'O' THEN 'Abierta' WHEN 'W' THEN 'Ganada' WHEN 'L' THEN 'Perdida' END 'Estado',
T3.Descript 'Ultima Etapa', T3.SlpName 'Empleado de Venta ultima etapa',
ISNULL(T5.Descript,'Ganada') 'Razón de Pérdida', T10.Descript 'Descripcion',
T7.Descript 'Fuente informacion', T8.IndName 'Ramo',T6.nAct 'Actividades', T6.lastDte 'Fecha ultima actividad' ,T0.U_ID_MP '# ID MP', T0.U_F_C_MP 'Fecha cierre licitacion MP', T0.U_H_C_MP 'Hora cierre licitacón', T0.U_F_A_MP 'Fecha adjudicacion MP', T0.U_Dur_Con 'Duracion contrato (meses)', CASE T0.U_T_Gar WHEN 1 THEN 'No requiere' WHEN 2 THEN 'Boleta garantia' WHEN 3 THEN 'Poliza garantia' END 'Tipo de garantia', T0.U_Monto_Gar 'Monto garantia', T0.U_Fecha_Gar 'Fecha entrega garantia',
T9.Name 'Competidor', T9.Amenaza 'Nivel de Amenaza', T9.Memo 'Cantidad', 'Alcapharma' 'Empresa'
FROM OOPR T0
INNER JOIN OSLP T1 ON T0.SlpCode = T1.SlpCode
INNER JOIN (SELECT t1.OpprId, t3.Descript, t5.SlpName FROM OPR1 t1 INNER JOIN
(SELECT OpprId, MAX(line) AS Linea FROM opr1 GROUP BY opprid) t2 ON (t1.OpprId = t2.OpprId AND t1.Line = t2.Linea)
INNER JOIN (SELECT opprid, Line, SlpCode FROM opr1) t4 ON (t2.OpprId = t4.opprid AND t2.Linea = t4.Line)
INNER JOIN OOST t3 ON t1.Step_Id = t3.StepId
INNER JOIN OSLP t5 ON T4.SlpCode = t5.SlpCode) T3 ON T0.OpprId = T3.OpprId
LEFT JOIN (SELECT t0.OpportId, t2.Descript FROM (SELECT OpportId, MIN(Line) AS Linea FROM opr5 GROUP BY OpportId) t0
INNER JOIN OPR5 t1 ON (t0.OpportId = t1.OpportId AND t0.Linea = t1.Line)
INNER JOIN OOFR t2 ON t1.ReasondId = t2.Num) T5 ON T0.OpprId = T5.OpportId
LEFT JOIN (SELECT ParentID, MAX(CntctDate) AS lastDte, COUNT(*) AS nAct FROM oclg GROUP BY ParentID) T6 ON T0.OpprId = T6.ParentID
LEFT JOIN OOSR T7 ON T0.Source = T7.Num
LEFT JOIN OOND T8 ON T0.Industry = T8.IndCode
LEFT JOIN (SELECT t0.Opportid, t1.Memo, t2.Name, CASE t1.ThreatLevl WHEN 1 THEN 'Baja' WHEN 2 THEN 'Media' WHEN 3 THEN 'Alta' END AS Amenaza
FROM (Select OpportId, MIN(Line) AS Linea from opr3 GROUP BY OpportId) t0 INNER JOIN opr3 t1 ON (t0.opportid = t1.opportid AND t0.linea = t1.line)
INNER JOIN OCMT t2 on t1.competid = t2.competid) T9 ON T0.OpprId = T9.OpportId
LEFT JOIN (SELECT t0.OprId, T2.Descript FROM (SELECT OprId, MIN(Line) AS Linea FROM OPR4 GROUP BY OprId) t0
INNER JOIN OPR4 t1 ON t0.OprId = t1.OprId AND t0.Linea = t1.Line
INNER JOIN OOIN t2 ON t1.IntId = t2.Num) T10 ON T0.Opprid = T10.OprId
ORDER BY T0.OpenDate
Saludos
User | Count |
---|---|
91 | |
7 | |
7 | |
4 | |
3 | |
3 | |
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.