473,386 Members | 1,753 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Combining several rows

Hello List!

I would like to combine the contents of several rows of a subquery. After
several hours of search in the documentation and the internet I didn'T find a
solution and hope anyone can help. My problem:

Let's say I've got a table in the following form:

SELECT * FROM test;

id | name
-----------
1 | abc
2 | def
3 | ghi

For a table like this I am looking for a query that returns a result that
looks this way:

name
-------------
abc, def, ghi

It should work for any number of rows. I would like to Insert the returned
String (with a comma separated list of all name-fields in the test-table) in
the main-query ( SELECT (whatever is a solution) AS name, other, fields FROM
tables...).

Thanks in advance

Matthias Nagl

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #1
5 1718
Matthias Nagl wrote:
Hello List!

I would like to combine the contents of several rows of a subquery. After
several hours of search in the documentation and the internet I didn'T find a
solution and hope anyone can help. My problem:

Let's say I've got a table in the following form:

SELECT * FROM test;

id | name
-----------
1 | abc
2 | def
3 | ghi

For a table like this I am looking for a query that returns a result that
looks this way:

name
-------------
abc, def, ghi


Joe Conway's crosstab() function in the 'tablefunc' contrib
directory of the source tree is probably what you want. If you've
installed by RPM, the postgresql-contrib package will have installed
the SQL script to initialize the function in
/usr/share/pgsql/contrib/tablefunc.sql.

HTH,

Mike Mascari
---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,
I would like to combine the contents of several rows of a subquery. After
several hours of search in the documentation and the internet I didn'T find
a solution and hope anyone can help. My problem:


You have to create your own aggreate.
You can use this one:

- ---------------------------------------------------
CREATE OR REPLACE FUNCTION
join_sep ( text, text, text )
RETURNS text
LANGUAGE 'sql'
IMMUTABLE
AS '
SELECT CASE
WHEN $1 IS NULL THEN $3
ELSE $1 || $2 || $3
END;
';
- ---------------------------------------------------
CREATE OR REPLACE FUNCTION
join_text ( text, text )
RETURNS text
LANGUAGE 'sql'
AS '
SELECT join_sep($1,'', '',$2);
';
- ---------------------------------------------------
CREATE AGGREGATE join (
BASETYPE = text,
SFUNC = join_text,
STYPE = text
);

Mit freundlichem Gruß / With kind regards
Holger Klawitter
- --
lists <at> klawitter <dot> de
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQFAsftE1Xdt0HKSwgYRAg/TAJ4rgGwjPVSwrudQ51NP8Imrw0OWhwCfUnMH
h/WlRt3eeNopOtDHYlslnw4=
=xA3Q
-----END PGP SIGNATURE-----
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #3
Matthias Nagl <pg@mnagl.de> writes:
Let's say I've got a table in the following form: SELECT * FROM test; id | name
-----------
1 | abc
2 | def
3 | ghi For a table like this I am looking for a query that returns a result that
looks this way: name
-------------
abc, def, ghi


The easy way to do this is with a user-defined aggregate. I believe
there are several threads in the mailing list archives that give
solutions to exactly this problem. I tried "create aggregate comma"
at http://www.pgsql.ru/db/pgsearch/ and got this as the first hit:
http://archives.postgresql.org/pgsql...3/msg00381.php

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #4

Matthias Nagl <pg@mnagl.de> writes:
For a table like this I am looking for a query that returns a result that
looks this way:

name
-------------
abc, def, ghi


You need something like this:

create function concat_agg_accum(varchar, varchar) returns varchar
as 'select $1 || '', '' || $2'
language sql
strict immutable;

create aggregate concat_agg (
basetype = varchar,
stype = varchar,
sfunc = concat_agg_accum
);
select concat_agg(name) as name, ...

--
greg
---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to ma*******@postgresql.org)

Nov 23 '05 #5
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Thanks a lot Holger your aggregate function is the perfect solution to my
problem.

yours
Matthias Nagl

Am Monday 24 May 2004 15:40 schrieb Holger Klawitter:
Hi,
I would like to combine the contents of several rows of a subquery. After
several hours of search in the documentation and the internet I didn'T
find a solution and hope anyone can help. My problem:


You have to create your own aggreate.
You can use this one:

---------------------------------------------------
CREATE OR REPLACE FUNCTION
join_sep ( text, text, text )
RETURNS text
LANGUAGE 'sql'
IMMUTABLE
AS '
SELECT CASE
WHEN $1 IS NULL THEN $3
ELSE $1 || $2 || $3
END;
';
---------------------------------------------------
CREATE OR REPLACE FUNCTION
join_text ( text, text )
RETURNS text
LANGUAGE 'sql'
AS '
SELECT join_sep($1,'', '',$2);
';
---------------------------------------------------
CREATE AGGREGATE join (
BASETYPE = text,
SFUNC = join_text,
STYPE = text
);

Mit freundlichem Gruß / With kind regards
Holger Klawitter

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAsnDLfg4gS83RRywRAmzWAJ9gzOVDbKsyjxB6tu08lD p6A+bT9wCfZ5QG
HOB3GYN85ldJJcvdH6W5F7I=
=Yw/g
-----END PGP SIGNATURE-----

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Ilan | last post by:
Hi all I need to add data from two Excel sheets (both on the same workbook) to an existing table in my SQL DB. The problem is that each sheet holds different fields for the same record, though...
3
by: Tome73 | last post by:
How can I easily add the rows of DataTable1 to the rows of DataTable2. Both queries are from the same table. I can always use the column names with myRow, but I was wishing for a shortcut. When I...
7
by: Barry | last post by:
Hi all, I've noticed a strange error on my website. When I print a capital letter P with a dot above, using & #7766; it appears correctly, but when I use P& #0775 it doesn't. The following...
3
by: Alex | last post by:
Hi all, We're importing data from a propriatery database, and below is a snippet of several lines: DateTime AcctNumber PtName Notes 12-23-2003 00432234 Smith, John Patient arrived from...
21
by: LoneStar | last post by:
Hello, I am tyring to combine several rows based on matching information..For example i have the following table. IOSC: FEATURE: 00029 LH 00029 SWFTERM 00029 WATS 00031 1PTY
5
by: KewlToyZ | last post by:
Good day, I am stuck in a strange situation. SQL 2000 Server, creating a stored procedure to use in Crystal Reports 11. I am trying to build a report without creating a table or temprorary table in...
10
by: ckeller06 | last post by:
i have 2 tables which are linked together table 1: { EventID, EventName, CustomerID } table 2: { CustomerID, CustomerFirstName, CustomerLastName, AGE } they are join by CustomerID...
3
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
1
by: dlee360 | last post by:
Hello! So I've been trying to figure out how to do the following in T-SQL: Orig Table: Col1 Col2 Col3 Set A ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.