473,395 Members | 1,941 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,395 software developers and data experts.

Aggregate/concatenate function (flattening columns into a row)

Hi Forum,

I'm trying to perform a type of concatenate function on a table and am not really sure where to start. I'd like to aggregate the values from 'field' on their ID. To explain, my table is like this:

ID roadnum
1 A-1
2 B-1
2 E-1
3 N-1
4 N-3
4 E-2
4 CA-1
5 E-1

Which I'd like to transform to:

ID roadnum
1 A-1
2 B-1 / E-1
3 N-1
4 E-2 / CA-1 / N-3
5 E-1

I've been looking at various lists and forums but I've not been able to follow the explanations. Further down the line, instead of simply concatenating the field values, I'd like to concatenate according to the road importance- which is based on the roadnum, but one step at a time. When I have the values in the same field, I'll worry about the next step.

Matt
Jun 12 '07 #1
4 23019
shoonya
161 100+
Hi Forum,

I'm trying to perform a type of concatenate function on a table and am not really sure where to start. I'd like to aggregate the values from 'field' on their ID. To explain, my table is like this:

ID roadnum
1 A-1
2 B-1
2 E-1
3 N-1
4 N-3
4 E-2
4 CA-1
5 E-1

Which I'd like to transform to:

ID roadnum
1 A-1
2 B-1 / E-1
3 N-1
4 E-2 / CA-1 / N-3
5 E-1

I've been looking at various lists and forums but I've not been able to follow the explanations. Further down the line, instead of simply concatenating the field values, I'd like to concatenate according to the road importance- which is based on the roadnum, but one step at a time. When I have the values in the same field, I'll worry about the next step.

Matt
you can use the array data tye
make the roadnum attribute an array and append the routes in the last
check array data type from postgreSQL documentation

but be careful as when you will select the array from databse using php or ny other language it will return only a string {value1,value2...}
so iterate nd make it an array

shoonya
Jun 14 '07 #2
michaelb
534 Expert 512MB
This should work for you

Expand|Select|Wrap|Line Numbers
  1. CREATE or replace FUNCTION flatten() RETURNS integer AS $$
  2. DECLARE
  3.     rec RECORD ;
  4.     cnt integer ;
  5.     delim varchar := '/';
  6. BEGIN
  7.      FOR rec IN SELECT * FROM tab1 ORDER BY ID LOOP
  8.         select count(1) into cnt from tab2 where tab2.ID = rec.ID;
  9.         if cnt = 0 then
  10.            insert into tab2 values (rec.ID, rec.roadnum);
  11.         else
  12.            update tab2 set roadnum = roadnum || delim || rec.roadnum 
  13.                where tab2.ID = rec.ID;
  14.         end if;
  15.     END LOOP;
  16.  
  17.     RETURN (select count (*) from tab2);
  18. END;
  19. $$ LANGUAGE plpgsql;
  20.  
here's the result of running select flatten();
Expand|Select|Wrap|Line Numbers
  1. postgres=# select * from tab1;
  2.   id   | roadnum
  3. -------+-----
  4.      1 | A1
  5.      2 | B1
  6.      2 | B2
  7.      3 | C1
  8.      3 | C2
  9.      3 | C3
  10.      4 | D1
  11.      4 | D2
  12.      4 | D3
  13.      4 | D4
  14.      5 | E1
  15.      6 | F1
  16.      7 | G1
  17.      7 | G2
  18.      8 | J1
  19.      8 | J2
  20.      9 | H1
  21.     10 | K1
  22. (18 rows)
  23.  
  24.  
  25. postgres=# select * from tab2;
  26.  id    | roadnum
  27. -------+-------------
  28.      1 | A1
  29.      2 | B1/B2
  30.      3 | C1/C2/C3
  31.      4 | D1/D2/D3/D4
  32.      5 | E1
  33.      6 | F1
  34.      7 | G1/G2
  35.      8 | J1/J2
  36.      9 | H1
  37.     10 | K1
  38. (10 rows)
  39.  
Jun 14 '07 #3
Liam
1
You may find this useful:
Expand|Select|Wrap|Line Numbers
  1. SELECT array_to_string(array(SELECT column_name FROM table_name), ':');
  2.  
Change the inner SELECT to any query that returns only one column.
This will concatenate all values into one string, with a ':' between each value.

Expand|Select|Wrap|Line Numbers
  1. SELECT array_to_string(array(SELECT roadnum FROM table_name WHERE ID=4), ':');
  2.  
would return "N-3:E-2:CA-1" .

Powerful!
Aug 8 '08 #4
CREATE TABLE ABC
(
ID int,
roadnum varchar(50)
)



INSERT INTO ABC VALUES (1,'A-1');
INSERT INTO ABC VALUES (2,'B-1');
INSERT INTO ABC VALUES (2,'E-1');
INSERT INTO ABC VALUES (3,'N-1');
INSERT INTO ABC VALUES (4,'N-3');
INSERT INTO ABC VALUES (4,'E-2');
INSERT INTO ABC VALUES (4,'CA-1');
INSERT INTO ABC VALUES (5,'E-1');

SELECT * FROM ABC;


SELECT DISTINCT ID,
ARRAY_TO_STRING(ARRAY(SELECT ROADNUM FROM ABC WHERE ID=T1.ID),'/') AS
ROADNUM
FROM ABC T1;
Aug 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Nick Heppleston | last post by:
I have a concatenation problem and I was wondering if somebody might be able to offer some help :-) I have the following table structure holding product long descriptions: Part...
6
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
1
by: Job Lot | last post by:
Is it possible to use Aggregate functions with GROUP BY Clauses on DataTable. I have a DataTable with following values: Date Amount Int Balance 1/1/2004 5000.00 50.00 5050.00...
1
by: sausage31 | last post by:
I have a table as follows.... Device LotID Result1 Result2 Result3 aaa 1 5 10 15 bbb 1 2 4 6 aaa 2 ...
3
by: MrNobody | last post by:
I've read that the expression property for DataColumns is used to "Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column.". I...
6
by: Larry Menard | last post by:
Folks, I know that DB2 does not (yet?) support this, but I wonder if anyone can suggest a work-around. I've seen article...
0
by: Matthew Lunnon | last post by:
Hi, I have two tables and I want to get summary information from the second table for each row of the first table, I can see two ways to do this one is with the SQL below but since the first table...
3
by: ncsthbell | last post by:
I am pulling my hair out on this! Seems like it should be easy, I just can not get it to work like I want. I know I am doing something wrong, so I hope someone can be so kind to guide me!!! I...
3
by: Michael Howes | last post by:
I have many double that each have a few thousand numbers in them. I need to concatenate groups of these double arrays into a new set of double. I don't know the total # of points. I thought it...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.