An example for WITH QUERY

From: Durumdara <durumdara(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: An example for WITH QUERY
Date: 2011-06-22 10:35:10
Message-ID: BANLkTikZwFa0A4khGXxzM-RZ5-Uezii2Hg@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

I have 3 tables. I want to run a query that collect some data from
them, and join into one result table.

I show a little example, how to do this in another DB with script:

create temp table tmp_a as select id, name, sum(cost) cost from items
where... group by id, name with data;

create temp table tmp_b as select item_id, sum(price) price from bills
where... group by item_id with data;

create temp table tmp_c as select item_id, sum(price) price from
incoming_bills where... group by item_id with data;

select
tmp_a.id, tmp_a.name, tmp_a.cost,
tmp_b.price outgoing_price,
tmp_c.price incoming_price
from tmp_a
left join tmp_b on (tmp_a.id = tmp_b.item_id)
left join tmp_c on (tmp_a.id = tmp_c.item_id)
order by name

This is very simple example. How I can write this in "WITH QUERY" form?

Thanks for every help!

dd

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Wim Bertels 2011-06-22 10:40:12 CRUD plpgsql generator
Previous Message simon 2011-06-22 09:42:42 Re: Error - could not get socket error status: Invalid argument