Ver Mensaje Individual
  #1 (permalink)  
Antiguo 23/04/2015, 13:18
marenas4001
 
Fecha de Ingreso: abril-2015
Mensajes: 3
Antigüedad: 9 años
Puntos: 0
Pregunta es posible usar subconsulta en lugar de UNION

Gracias de antemano por sus consejos.
Nesito crear una consulta que genere un estado de cuenta para clientes,
una linea seran los datos de la factura y la/las siguientes con los pagos aplicados a esas mismas facturas.
Estoy tratando de filtrar por el campo entity y no hace el filtro.
obvio que esoy generando mal la consulta.
Es posible utilizar subconsultas en lugar de esta UNION

Código SQL:
Ver original
  1. SELECT
  2. f.rowid AS rowid,
  3. f.datef AS datef,
  4.     CASE f.TYPE
  5.         WHEN 0 THEN
  6.             'Factura'
  7.         WHEN 2 THEN
  8.             'Credito'
  9.         ELSE
  10.             'Desconocido'
  11.         END
  12.      AS Tipo_Doc,
  13. f.rowid AS facid,
  14. f.facnumber AS facnumber,
  15. f.fk_soc AS fk_soc,
  16. s.nom AS nom,
  17. IF ((   f.total_ttc >= 1    ),f.total_ttc,'') AS Cargo,
  18. IF ((   f.total_ttc <= 1    ),f.total_ttc,'') AS Abono,
  19.  s.entity AS entity
  20. FROM llx_facture AS f   JOIN llx_societe AS s ON f.fk_soc = s.rowid
  21. WHERE   f.fk_statut BETWEEN 1   AND 2
  22.  
  23. UNION ALL
  24.  
  25. SELECT
  26. p.rowid AS rowid,
  27. p.datep AS datep,
  28. 'PAGO' AS Tipo_Doc,
  29. pf.fk_facture AS facid,
  30. f.facnumber AS facnumber,
  31. f.fk_soc AS fk_soc,
  32. s.nom AS nom,
  33. IF ((pf.amount <= 1),pf.amount,'') AS Cargo,
  34. IF ((pf.amount >= 1),pf.amount,'') AS Abono,
  35. s.entity AS entity
  36. FROM llx_paiement AS p JOIN llx_paiement_facture AS pf ON p.rowid = pf.fk_paiement
  37. JOIN llx_facture AS f ON pf.fk_facture = f.rowid
  38. JOIN llx_societe AS s ON f.fk_soc = s.rowid
  39. WHERE s.entity = 21
  40. ORDER BY facid

Anexo fragmento del resultado separado por comas:

rowid,datef,Tipo_Doc,facid,facnumber,fk_soc,nom,Ca rgo,Cargo,entity
75,2/2/2015 00:00:00,Factura,75,FAC-00001,44,ORGANIZACIÓN AURORA S.A DE C.V,1900.00000000,,18
76,2/2/2015 00:00:00,Factura,76,FAC-00002,44,ORGANIZACIÓN AURORA S.A DE C.V,750.00000000,,18
80,2/2/2015 00:00:00,Credito,80,NCR-00001,44,ORGANIZACIÓN AURORA S.A DE C.V,,-1900.00000000,18
81,2/2/2015 00:00:00,Credito,81,NCR-00002,44,ORGANIZACIÓN AURORA S.A DE C.V,,-750.00000000,18
90,2/2/2015 00:00:00,Factura,90,FAC-00004,43,VERONICA SANTOS JOACHIN,500001.25000000,,18
92,2/2/2015 00:00:00,Factura,92,FAC-00005,43,VERONICA SANTOS JOACHIN,590000.18000000,,18
39,20/2/2015 12:00:00,PAGO,192,FAC-00035,106,PROMOTORA HOTELERA ORIGINAL SA DE CV,,638.12000000,21
192,16/2/2015 00:00:00,Factura,192,FAC-00035,106,PROMOTORA HOTELERA ORIGINAL SA DE CV,638.12000000,,21
28,19/2/2015 12:00:00,PAGO,193,FAC-00036,100,GRUPO MAJOSA SA DE CV,,939.60000000,21
193,17/2/2015 00:00:00,Factura,193,FAC-00036,100,GRUPO MAJOSA SA DE CV,939.60000000,,21
194,11/2/2015 00:00:00,Factura,194,FAC-01961,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,31237.18000000,,17
196,11/2/2015 00:00:00,Factura,196,FAC-01963,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,35253.79000000,,17
198,12/2/2015 00:00:00,Factura,198,FAC-01964,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,63336.00000000,,17
199,12/2/2015 00:00:00,Factura,199,FAC-01965,19,GRUPO INDUSTRIAL CREYSI, S.A. DE C.V.,8184.96000000,,17

Última edición por gnzsoloyo; 23/04/2015 a las 13:31