Thread: how to "group" several records with same timestamp into one line?
Hi everyone,
My data with same timestamp "2008-11-12 12:12:12" in postgresql are as follows
rowid data unit channel create_on
------------------------------------------------------------------------------
1 1.5 MPa channel1 2008-11-12 12:12:12
2 2.5 M3 channel2 2008-11-12 12:12:12
3 3.5 M3 channel3 2008-11-12 12:12:12
4 4.5 t channel4 2008-11-12 12:12:12
------------------------------------------------------------------------------
I would like to "group" them into one line with SQL like
1.5 MPa 2.5 M3 3.5 M3 4.5 t 2008-11-12 12:12:12
The table is defined as
CREATE TABLE record_data
(
rowid serial NOT NULL,
data double precision,
unit character(10),
(
rowid serial NOT NULL,
data double precision,
unit character(10),
channel character(20),
create_on timestamp
create_on timestamp
)
Is that possible? Thanks for your help in advance.
Ouyang
zxo102 ouyang wrote: > I would like to "group" them into one line with SQL like > > 1.5 MPa 2.5 M3 3.5 M3 4.5 t 2008-11-12 12:12:12 Look up the "GROUP BY" clause. http://www.postgresql.org/docs/current/static/sql-select.html Note that with timestamps you may have to truncate/round them to the desired precision, since current versions of PostgreSQL store timestamps in a floating point format and two timestamps you'd expect to be equal may not end up being seen as exactly equal. -- Craig Ringer
On Wed, Nov 12, 2008 at 3:59 AM, zxo102 ouyang <zxo102@gmail.com> wrote: > Hi everyone, > My data with same timestamp "2008-11-12 12:12:12" in postgresql are as > follows > > rowid data unit channel create_on > ------------------------------------------------------------------------------ > 1 1.5 MPa channel1 2008-11-12 12:12:12 > 2 2.5 M3 channel2 2008-11-12 12:12:12 > 3 3.5 M3 channel3 2008-11-12 12:12:12 > 4 4.5 t channel4 2008-11-12 12:12:12 > ------------------------------------------------------------------------------ > > I would like to "group" them into one line with SQL like > > 1.5 MPa 2.5 M3 3.5 M3 4.5 t 2008-11-12 12:12:12 > Lookup the crosstab contrib functions.