Re: Select and merge rows?

From: Claudio Adriano Guarracino <elninon(at)yahoo(dot)com>
To: pgsql-sql(at)postgresql(dot)org, "scorpdaddy(at)hotmail(dot)com" <scorpdaddy(at)hotmail(dot)com>
Subject: Re: Select and merge rows?
Date: 2011-05-06 01:06:14
Message-ID: 597398.41914.qm@web161309.mail.bf1.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Thank you very much!
Your example help me a lot!
The original query is more complex, but I can continue with this example.
Thanks again!

--- On Thu, 5/5/11, scorpdaddy(at)hotmail(dot)com <scorpdaddy(at)hotmail(dot)com> wrote:

From: scorpdaddy(at)hotmail(dot)com <scorpdaddy(at)hotmail(dot)com>
Subject: Re: [SQL] Select and merge rows?
To: "Claudio Adriano Guarracino" <elninon(at)yahoo(dot)com>, pgsql-sql(at)postgresql(dot)org
Date: Thursday, May 5, 2011, 5:41 PM

While there is insufficient information provided (a couple of table snippets), you may consider and experiment with the snippet below to get you started.

SELECT
 ids.id,
 f1.value AS value1,
 f2.value AS value2,
 f3.value AS value3
FROM
( SELECT DISTINCT id FROM foo ) AS ids
LEFT JOIN foo f1
ON f1.id = ids.id
AND f1.order = 1
LEFT JOIN foo f2
ON f2.id = ids.id
AND f2.order = 2
LEFT JOIN foo f3
ON f3.id = ids.id
AND f3.order = 3
ORDER BY ids.id;

----- Reply message -----
From: "Claudio Adriano Guarracino" <elninon(at)yahoo(dot)com>
Date: Thu, May 5, 2011 5:18 pm
Subject: [SQL] Select and merge rows?
To: <pgsql-sql(at)postgresql(dot)org>

Hello!
I have a doubt about a query that I tried to do, but I cant......
This is the scenario:
I have a table, with this rows:
order    ID    value   
----------------------
1    1000    3
2    1000    5
3    1000    6
1    1001    1
2    1001    2
1    1002    4
2    1002    4

I need to get this table, divided by ID, like this:
id    value1    value2    value3
--------------------------------
1000    3    5    6
1001    1    2
1002    1    2

How I can do this?
I tried with cursors and view, but i can't....
Any help is welcome!

Thanks in advance!
Regards,

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Tarlika Elisabeth Schmitz 2011-05-06 05:06:08 check constraint bug?
Previous Message Rob Sargent 2011-05-05 23:15:41 Re: slightly OT - Using psql from Emacs with sql.el