From: | Arturo <arturomunive(at)gmail(dot)com> |
---|---|
To: | angel Nuñez Conde <afiladostoledo(at)gmail(dot)com> |
Cc: | pgsql-es-ayuda(at)postgresql(dot)org |
Subject: | Re: Como agrupar por fechas dos tablas distintas |
Date: | 2009-09-06 15:58:40 |
Message-ID: | c5b90f20909060858y433fa918xeb2ea8dcee9f30a9@mail.gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-es-ayuda |
Creo que debes hacer una operacion con UNION ALL y sobre ese resultado
puedes operar como sobre una tabla
El 6 de septiembre de 2009 05:15, angel Nuñez Conde <
afiladostoledo(at)gmail(dot)com> escribió:
> Hola.
>
> Tengo 2 tablas con los mismos campos y no estan relacionadas.
>
> Ventas Compras
> ------- --------
> fecha fecha
> subtotal subtotal
>
> quiero hacer una consulta que me agrupe por años y meses las dos tablas
> con estos campos:
>
> año mes totalventas totalcompras beneficios
>
> la consulta que intento y me da datos erroneos (las sumas son
> exagerada ) es la siguiente:
>
> SELECT
> date_part('year',fv.fecha) AS año,
> date_part('month',fv.fecha) AS mes,
> Sum(fv.subtotal) AS totalventas,
> sum(fc.subtotal) as totalcompras,
> Sum(fv.subtotal) - Sum(fc.subtotal) as beneficios
> FROM w_totalfacturascompras AS fc
> INNER JOIN w_totalfacturasventas AS fv
> on date_part('month',fv.fecha) = date_part('month',fc.fecha) and
> date_part('year',fv.fecha)= date_part('year',fc.fecha)
> group by date_part('month',fv.fecha),date_part('year',fv.fecha)
> having date_part('year',fv.fecha) > 2000
> order by 1,2
>
>
> ¿Como agrupo las fechas de las dos tablas para que las sumas sean
> correctas?.
>
> Un saludo Angel
>
>
>
> --
> TIP 5: ¿Has leído nuestro extenso FAQ?
> http://www.postgresql.org/docs/faqs.FAQ.html
>
From | Date | Subject | |
---|---|---|---|
Next Message | Arturo | 2009-09-06 15:59:21 | Re: Como agrupar por fechas dos tablas distintas |
Previous Message | angel Nuñez Conde | 2009-09-06 10:15:05 | Como agrupar por fechas dos tablas distintas |