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

Multi dimensional arrays, sub selects...

4
Hi All,

I have a system that stores information about a person, name addresses and so on and I would like to return a single row for each person in an SQL statement. This is so it can be placed in a view making it easy to obtain all information for person x.

Since a person can have more than one address I thought I could build an array holding a row for each address with each row also been an array to store the fields.

The schema is a person table with their name and id in it and an addresses table that holds the address information:

Person:
int8 id,
text name
...

Addresses
int8 id,
int8 personid,
text line1,
text line2,
text town,
...

For example the result I would like is

1,Jon, Doe, { {1 street,some place,some city}, {2 drive,some other place, some other city} }

However I cannot figure out the SQL to achieve this.

For the SQL I had something like this:

SELECT p.id, p.name,

ARRAY[COALESCE(a.line1,''),
COALESCE(a.line2 ,''),
COALESCE(a.Town,''),
] AS "Address",


FROM People AS p
JOIN Addresses AS a ON a.PersonId=p.id;

(Coalesce since cannot have nulls in arrays :( )

Which works in getting a single address into an array. Of course if the person has more than one address I get two rows returned for person x.

Attempting to re-work the SQL I could not get anything to run, the closest been:

SELECT p.id, p.name,

ARRAY[ ( SELECT COALESCE(a.line1,''),
COALESCE(a.line2 ,''),
COALESCE(a.Town,''),
FROM Addresses AS a ON a.PersonId=p.Id ) ] AS Addresses

FROM People AS p

Which obviously fails if there is more than one address.

Suggestions or ideas would be welcome.

Thanks
Feb 9 '07 #1
2 2689
MarkD
4
So I take that no one has any ideas?

Thanks.
Feb 12 '07 #2
michaelb
534 Expert 512MB
Sorry, Mark,
this is not something I can answer without first trying some code, unfortunately the last few days were crazy and I still have no spare time...

I guess you may have already checked out the manual on array functions and operators?

http://www.postgresql.org/docs/8.0/s...ons-array.html

http://www.postgresql.org/docs/8.0/static/arrays.html

Michael.
Feb 13 '07 #3

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

Similar topics

0
by: James | last post by:
I see that variations on this question have appeared before but I'm still completely stumped. I'm developing an application with a fairly robust graphics component for 3D rendering. I've written...
2
by: ip4ram | last post by:
I used to work with C and have a set of libraries which allocate multi-dimensional arrays(2 and 3) with single malloc call. data_type **myarray =...
5
by: Cant Think Today | last post by:
I have multi-dimesional arrays that can be specifed by the user, e.g 1,2,3,4,5 1,2,3,4,5,6,7,8,9,10 1,2,3,4,5,6 I think a bit of code that will iterate over these arrays to print out the...
4
by: Richard Hayden | last post by:
Hi, Why does gcc (3.3.2) give me a 'initialization from incompatible pointer type' warning when compiling: int main(int argc, char** argv) { int testa; int** testp = testa; }
8
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
4
by: Robert P. | last post by:
I can easily store a one-dimensional array in viewstate ( see Test1 ) If I try storing a multi-dimensional array in the viewstate it's crapping out on me when it goes to serialize the array (not...
4
by: entitledX | last post by:
Hi, I'm trying to use the HDF library to read a few HDF files that I need to process. The data in each file varies in rows, but the columns remain constant. Because of that, I had dynamically...
4
by: Balaskas Evaggelos | last post by:
Hi, does anyone know how i can sort a multi-dimensional array by a specific field ? for example i want to sort arr where n=2, but i need the data of every array to follow that order. ...
4
by: =?Utf-8?B?SGVucmlrIFNjaG1pZA==?= | last post by:
Hi, consider the attached code. Serializing the multi-dimensional array takes about 36s vs. 0.36s for the single-dimensional array. Initializing the multi-dimensional array takes about 4s...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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
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
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...

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.