Go Back  HTML Forums - Free Webmaster Forums and Help Forums > WEBSITE DEVELOPMENT > Databasing
User Name:
Password:
 

Reply
Thread Tools   Display Modes
  View First Unread
 
Old 11-19-2008, 09:55 AM
  #1
Validus
Soldier (Level 11)
 
Validus's Avatar
 
Join Date: May 2001
Location: Winnipeg, Canada
Posts: 132
iTrader: (0)
Validus is on a distinguished road
Grouping text rows in PostgreSQL

Hi,

What I'm trying to achieve is this:

id | name | value1
id | name | value2
id | name | value3

To look like this:

id | name | value1, value2, value3

I've been trying to use array_to_string, but having trouble with it because sometimes the third column is empty so I'm getting NULL array errors.

Any ideas

Added info:

Here's more or less what I've got right now:

Code:
SELECT
        site.name,
        site_contact.phone,
        site_contact.fax,
        site_contact.lastname || ' ' || site_contact.firstname AS contact_person,
        site_contact.email
FROM
        site
LEFT OUTER JOIN
        site_contact
ON
        site.siteid = site_contact.siteid
WHERE
        site.code
IN
        (list here)
ORDER BY
        site.sitename ASC;
Now this understandably gives me results like so:

sitename | phone1 | | person1 | email1
sitename | | | person2 |
sitename | | | person3 |
sitename | phone4 | | person4 | email4

I would like to get this in a single row:

sitename | phone1, phone4 | | person1, person2, person3, person4 | email1, email4

What I had tried was something like this instead of the JOIN:

Code:
array_to_string(
                array(
                        SELECT site_contact.phone FROM site_contact WHERE site_contact.siteid = site.siteid
                ), ', '
        )
But it didn't work because of the NULL fields.
__________________
-Val
The Broken Lands

Last edited by Validus : 11-19-2008 at 10:32 AM.
Validus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-19-2008, 11:43 AM
  #2
putts
SELECT MINE FROM TITLES
 
putts's Avatar
 
Join Date: Nov 2002
Location: Frankenmuth, MI
Posts: 4,918
iTrader: (0)
putts has a spectacular aura aboutputts has a spectacular aura about
If the root of your problem is the existence of NULL values have you tried just doing a COALESCE() on your columns to remove the NULLs?

To use COALESCE do this ..... COALESCE(column-name,'').
What COALESCE does is go through the list of values sent in until it finds a non-null value. So, to simply replace any NULLs with Empty Strings you can do something similar to my sample statement.

At that point, you should be able to do your array_to_string .... which I gather is Postgre's equivalent to mySQL's GROUP_CONCAT (and MS SQL's CROSS APPLY)?

btw, does Postgre really use || for string concats? Really??? That's just craziness
__________________
Signature Image Rotator will return when I have time to fix it

Running Scoreboard
Anthony : 3 points
putts is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Old 11-19-2008, 12:27 PM
  #3
Validus
Soldier (Level 11)
 
Validus's Avatar
 
Join Date: May 2001
Location: Winnipeg, Canada
Posts: 132
iTrader: (0)
Validus is on a distinguished road
Thanks putts! Coalesce() did the trick.

And yeah, it does. Had me quite confused too.
__________________
-Val
The Broken Lands
Validus is offline   Add to del.icio.us Add to del.icio.us    Can you digg it?Can you digg it? Reply With Quote
Reply
« Previous Thread | Next Thread »


 
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
  
 
 
 



 
  POSTING RULES
 
 
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Thread Tools
Display Modes

Forum Jump

 

All times are GMT -5. The time now is 10:17 PM.

   

Mascot team created by Drawshop.com

Powered by vBulletin® Version 3.6.7
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.

Server Monitoring by ENIACmonitor 0.01
HTMLforums.com © Big Resources, Inc. Web Design by BoxedArt.com
vRewrite 1.5 beta SEOed URLs completed by Tech Help Forum and Chalo Na.