Re: Consulta compleja

From: "Jorge Vidal Martinez" <jvm(at)utenet(dot)com(dot)ar>
To: "Ricardo Fuentes Pereira" <rfuentesp(at)gmail(dot)com>
Cc: <pgsql-es-ayuda(at)postgresql(dot)org>
Subject: Re: Consulta compleja
Date: 2005-06-01 00:07:49
Message-ID: 002601c5663d$fa10cb50$0176fea9@XP
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-es-ayuda

Ricardo:
Efectivamente, hace esto

.......rut ....... | .nombre..| num_fact |.......rut ....... | valor_neto
| valor_iva | num_fact | rut | valor_neto | valor_iva
-------------+----------+---------+--------------+-----------+---------+----------+-------------+-----------+---------
11111111-1 | Ricardo .| ............1 | 11111111-1 | 150 |
0.19 | 1 | 11111111-1 | 100 | 0.19
11111111-1 | Ricardo .| ............1 | 11111111-1 | 150 |
0.19 | 2 | 11111111-1 | 200 | 0.19
11111111-1 | Ricardo .| ............2 | 11111111-1 | 250 |
0.19 | 1 | 11111111-1 | 100 | 0.19
11111111-1 | Ricardo .| ........... 2 | 11111111-1 | 250 |
0.19 | 2 | 11111111-1 | 200 | 0.19
22222222-2 | Andres | ............3 | 22222222-2 | 50 |
0.19 |
22222222-2 | Andres | ............4 | 22222222-2 | 300 |
0.19 |

rut | nombre | sum_valor
------------+---------+-----------
11111111-1 | Ricardo | 1666 - Erroneo - cuando tiene facturas de
compras y de gastos hace N * N.
22222222-2 | Andres | 416.5 - Bien - porque solo tiene valores en
una tabla.

Perdon, otra oportunidad

SELECT AA.rut, AA.nombre , AA.valor1 + BB.valor2
FROM ( SELECT A.rut, A.nombre, ( COALESCE(SUM(B.valor_neto * (1 +
B.valor_iva)), 0) as valor1
FROM sx_prov A LEFT JOIN
sx_fact_compra B ON A.rut = B.rut
GROUP BY A.rut, A.nombre ) AA LEFT JOIN
( SELECT rut ,COALESCE(SUM(valor_neto * (1 + valor_iva)),
) AS valor2
FROM fact_gasto
GROUP BY rut ) BB on AA.rut = B.rut

---- Original Message -----
From: "Ricardo Fuentes Pereira" <rfuentesp(at)gmail(dot)com>
To: "Jorge Vidal Martinez" <jvm(at)utenet(dot)com(dot)ar>
Cc: <pgsql-es-ayuda(at)postgresql(dot)org>
Sent: Tuesday, May 31, 2005 10:49 AM
Subject: Re: [pgsql-es-ayuda] Consulta compleja

> Jorge Vidal Martinez escribió:
>
>> Ricardo, estas totalizado las las sx_fact_compra y las sc_fact_gastos por
>> RUC y luego al hacer join con las facturas y agruparlas por un atributo
>> de esta tabla te repite este total como tantas facturas tengas.
>>
>> Intenta
>>
>> SELECT
>> A.ruc, A.nombre ,
>> ( COALESCE(SUM(B.valor_neto * (1 + B.valor_iva)), 0) +
>> COALESCE(SUM(C.valor_neto * (1 +C. valor_iva)), 0) ) AS sum_valor
>> FROM
>> sx_prov prov A
>> LEFT JOIN
>> sx_fact_compra B ON A.rut = B.rut
>> LEFT JOIN
>> sx_fact_gasto C ON A.rut = C.rut
>> GROUP BY
>> A.ruc, A.nombre
>>
>> El LEFT te devuelve todas las tuplas que esten en la tabla primaria
>> esten o no en la secundaria..
>>
>> Por lo que los proveedores que no tengan facturas de Compras o Gastos, te
>> aparecen en el resultado.
>>
>> Saludos
>> Jorge
>>
> Hola. Gracias por responder.
> Lo probé, pero para los sgtes datos:
> test=# select * from sx_prov;
> rut | nombre
> ------------+---------
> 11111111-1 | Ricardo
> 22222222-2 | Andres
> (2 filas)
>
> test=# select * from sx_fact_compra;
> num_fact | rut | valor_neto | valor_iva
> ----------+------------+------------+-----------
> 1 | 11111111-1 | 150 | 0.19
> 2 | 11111111-1 | 250 | 0.19
> 3 | 22222222-2 | 50 | 0.19
> 4 | 22222222-2 | 300 | 0.19
> (4 filas)
>
> num_fact | rut | valor_neto | valor_iva
> ----------+------------+------------+-----------
> 1 | 11111111-1 | 100 | 0.19
> 2 | 11111111-1 | 200 | 0.19
> (2 filas)
>
> me devuelve
>
> test=# SELECT
> test-# A.rut, A.nombre ,
> test-# ( COALESCE(SUM(B.valor_neto * (1 + B.valor_iva)), 0) +
> test(# COALESCE(SUM(C.valor_neto * (1 +C. valor_iva)), 0) ) AS
> sum_valor
> test-# FROM
> test-# sx_prov A
> test-# LEFT JOIN
> test-# sx_fact_compra B ON (A.rut = B.rut)
> test-# LEFT JOIN
> test-# sx_fact_gasto C ON (A.rut = C.rut)
> test-# GROUP BY
> test-# A.rut, A.nombre ;
> rut | nombre | sum_valor
> ------------+---------+-----------
> 11111111-1 | Ricardo | 1666
> 22222222-2 | Andres | 416.5
> (2 filas)
>
> Lo cual es erroneo para el caso del rut 11111111-1 pues debiera ser 833,
> como si es correcto con mi otra consulta (ver mail anterior)
>
> Saludos.
>
>
>
> ---------------------------(fin del mensaje)---------------------------
> TIP 3: si publicas/lees desde Usenet, por favor envía "subscribe-nomail"
> a majordomo(at)postgresql(dot)org para que tus mensajes puedan llegar
> a los suscriptores de la lista
>
>
>
> --
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.322 / Virus Database: 267.3.3 - Release Date: 31/05/2005
>
>

In response to

Responses

Browse pgsql-es-ayuda by date

  From Date Subject
Next Message Ricardo David Carrillo Sánchez 2005-06-01 03:53:32 Consulta ..
Previous Message Roberto Cesar Najera 2005-05-31 22:05:48 insertar bytea