on 04-17-2013 7:00 PM
Buenas tardes jóvenes tengo el siguiente inconveniente estoy realizando una unión de facturación de ventas y pedidos de ventas que estén en status abierto, la unión si me la realiza perfectamente pero cuando existen dos items o ya sea en facturación o en pedido no lo deja en la misma linea sino que me genera otra linea, les dejo el query para que lo revisen y me indiquen en donde esta mi problema
SELECT T3.DocNum,T0.[CardName], T0.[CardCode], T0.[U_Dirmuni], T0.[U_Fecha_escritura],
T0.[U_Feconta], T3.[Comments], T3.[NumAtCard], T4.[U_Lecturaactual],
T4.[U_Lecturaanterior], (T4.[U_Lecturaactual]-T4.[U_Lecturaanterior])[Consumo Mts2],
T0.[U_Derecho_Mts3],
Case When (T4.[U_Lecturaactual]-T4.[U_Lecturaanterior])> T0.[U_Derecho_Mts3] Then ((T4.[U_Lecturaactual]-T4.[U_Lecturaanterior])-T0.[U_Derecho_Mts3]) Else '0' end [Exceso Mts2],
T0.[U_Costo_Exce], T0.[U_Costo_agua], T0.[U_Costo_Seg],
((Case When (T4.[U_Lecturaactual]-T4.[U_Lecturaanterior])> T0.[U_Derecho_Mts3] Then ((T4.[U_Lecturaactual]-T4.[U_Lecturaanterior])-T0.[U_Derecho_Mts3]) Else '0' end)* T0.[U_Costo_Exce])[Exceso Valor],
--Case When T4.ItemCode like 'EXC%%' then T4.PriceAfVAT end [Costo Exceso],
Case When T4.ItemCode like 'AGU%%' then T4.PriceAfVAT end [Costo Cuota Fija],
Case When T4.ItemCode like 'SEG%%' then T4.PriceAfVAT end [Costo Seguridad],
Case When T4.ItemCode like 'RCONX%%' then T4.PriceAfVAT end [Costo Reconexión],
Case When T4.ItemCode like 'SUSP%%' then T4.PriceAfVAT end [Costo Suspención],
Case When T4.ItemCode like 'ADM%%' then T4.PriceAfVAT end [Administrativo], T3.[DocTotal]
FROM OCRD T0 INNER JOIN
OINV T3 ON T0.CardCode = T3.CardCode INNER JOIN
INV1 T4 ON T3.DocEntry = T4.DocEntry
WHERE /*T0.[U_subproyecto] = 'PL05' and*/ T3.[DocStatus]= 'O' and T4.[U_Lecturaanterior]>'0'and T0.[CardCode]= '0060502020'
GROUP BY T0.[CardName], T0.[CardCode], T0.[U_Dirmuni], T0.[U_Fecha_escritura],
T0.[U_Feconta], T3.[Comments], T3.[NumAtCard], T4.[U_Lecturaactual],
T4.[U_Lecturaanterior], T0.[U_Costo_Exce], T0.[U_Costo_agua], T0.[U_Costo_Seg],
T3.[DocTotal], T3.[Comments], T3.[NumAtCard], T4.[U_Lecturaactual],
T4.[U_Lecturaanterior], T3.[DocStatus], T3.[DocStatus], T4.[ItemCode], /*T2.[ItemCode],*/
T4.[PriceAfVat], T3.DocNum, T0.[U_subproyecto], T3.CardCode, T0.[U_Derecho_Mts3]
UNION ALL
SELECT T1.DocNum,T0.[CardName], T0.[CardCode], T0.[U_Dirmuni], T0.[U_Fecha_escritura],
T0.[U_Feconta], T1.[Comments], T1.[NumAtCard], T2.[U_Lecturaactual],
T2.[U_Lecturaanterior], (T2.[U_Lecturaactual]-T2.[U_Lecturaanterior])[Consumo Mts2],
T0.[U_Derecho_Mts3],
Case When (T2.[U_Lecturaactual]-T2.[U_Lecturaanterior])> T0.[U_Derecho_Mts3] Then ((T2.[U_Lecturaactual]-T2.[U_Lecturaanterior])-T0.[U_Derecho_Mts3]) Else '0' end [Exceso Mts2],
T0.[U_Costo_Exce], T0.[U_Costo_agua], T0.[U_Costo_Seg],
((Case When (T2.[U_Lecturaactual]-T2.[U_Lecturaanterior])> T0.[U_Derecho_Mts3] Then ((T2.[U_Lecturaactual]-T2.[U_Lecturaanterior])-T0.[U_Derecho_Mts3]) Else '0' end)* T0.[U_Costo_Exce])[Exceso Valor],
--Case When T2.ItemCode like 'EXC%%' then T2.PriceAfVAT end [Costo Exceso],
Case When T2.ItemCode like 'AGU%%' then T2.PriceAfVAT end [Costo Cuota Fija],
Case When T2.ItemCode like 'SEG%%' then T2.PriceAfVAT end [Costo Seguridad],
Case When T2.ItemCode like 'RCONX%%' then T2.PriceAfVAT end [Costo Reconexión],
Case When T2.ItemCode like 'SUSP%%' then T2.PriceAfVAT end [Costo Suspención],
Case When T2.ItemCode like 'ADM%%' then T2.PriceAfVAT end [Administrativo], T1.[DocTotal]
FROM OCRD T0 INNER JOIN
ORDR T1 ON T0.CardCode = T1.CardCode INNER JOIN
RDR1 T2 ON T1.DocEntry = T2.DocEntry
WHERE /*T0.[U_subproyecto] = 'PL05' and*/ T1.[DocStatus] = 'O' and T2.[PriceAfVAT]>'0' and T0.[CardCode]= '0060201006'
GROUP BY T0.[CardName], T0.[CardCode], T0.[U_Dirmuni], T0.[U_Fecha_escritura],
T0.[U_Feconta], T0.[U_Costo_Exce], T0.[U_Costo_agua], T0.[U_Costo_Seg],
T2.[ItemCode], T2.[PriceAfVat], T1.[Comments], T1.[NumAtCard], T2.[U_Lecturaactual],
T2.[U_Lecturaanterior],T1.[DocTotal], T1.DocNum, T0.[U_subproyecto], T0.[U_Derecho_Mts3]
ORDER BY T0.[CardCode]
Ejemplo este query me lo muestra de la siguiente forma
Nombre Consumo Exceso
Thelma Ordoñes 100
Thelma Ordones 0.00 50.00
y me gustaria que apareciera asi:
Nombre Consumo Exceso
Thelma Ordoñes 100 50
Espero me puedan ayudar y de antemano muy agradecido por su ayuda.
Saludos cordiales
Intenta creando un nuevo SELECT y ese que tienes sea la fuenta (FROM) de tu nuevo SELECT. Algo así:
SELECT TA.* from (
SELECT T0.CardName, T0.Consumo, T0.Exceso from OINV T0
union all
SELECT T0.CardName, T0.Consumo, T0.Exceso from ORIN T0
) TA
Esa es la primera parte, para que muestre el resultado que buscas, ajustar el query haciendo sumatorias y agrupaciones:
SELECT TA.CardName, sum(TA.Consumo) as Consumo, sum(TA.Exceso) as Exceso from
(SELECT T0.CardName, T0.Consumo, T0.Exceso from OINV T0
union all
SELECT T0.CardName, T0.Consumo, T0.Exceso from ORIN T0) TA
group by TA.CardName
Suerte y saludos,
Juan Carlos.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
108 | |
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.