![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| ||||
| ||||
|
|
create table person ( person_id serial not null, primary key (person_id), name text not null ); create table marriage ( marriage_id serial not null, primary key (marriage_id), husband_id int not null, foreign key (husband_id) references person (person_id), wife_id int not null, foreign key (wife_id) references person (person_id), marriage_date timestamp null ); |
|
select distinct person.person_id, person.name, marriage.marriage_id, marriage.marriage_date from person left join marriage on (person.person_id = marriage.husband_id or person.person_id = marriage.wife_id) order by person.name, marriage.marriage_date; |
|
person_id name marriage_id marriage_date 34 fred 124 2002-01-23 212 sally 63 1995-06-01 212 sally 1221 2004-08-01 169 george 143 1988-02-22 |
|
person_id name marriage_id_1 marriage_date_1 marriage_id_2 marriage_date_2 34 fred 124 2002-01-23 212 sally 63 1995-06-01 1221 2004-08-01 169 george 143 1988-02-22 |
#2
| |||
| |||
|
|
WHAT I WOULD LIKE TO DO, is to collapse the rows of multiple people into one row and have a query result that looks like this: person_id name marriage_id_1 marriage_date_1 marriage_id_2 marriage_date_2 34 fred 124 2002-01-23 212 sally 63 1995-06-01 1221 2004-08-01 169 george 143 1988-02-22 no matter how many marriages any one person may have had. Can such a query be written entirely with SQL? Or is there a common stored procedure that people use? Thanks! |
#3
| |||
| |||
|
|
CREATE AGGREGATE textcat_all( basetype = text, sfunc = textcat, stype = text, initcond = '' ); |
|
select person.person_id, person.name, textcat_all( marriage.marriage_id || '; ') as marriage_ids, textcat_all( marriage.marriagedate || '; ') as marriage_dates from person left join marriageon (person.person_id = marriage.husband_id or person.person_id = marriage.wife_id) group by person.person_id, person.birthdate order by person.name; |
|
select person.person_id, person.name, textcat_all( blah_blah.marriage_id || '; ') as marridage_ids, textcat_all( blah_blah.marriagedate || '; ') as marriage_dates from person left join ( select marriage.marriage_id, marriage.marriage_date, marriage.husband_id, marriage.wife_id from marriage order by marriage.marriage_date ) blah_blah on (person.person_id = blah_blah.husband_id or person.person_id = blah_blah.wife_id) group by person.person_id, person.birthdate order by person.name ; |
#4
| |||
| |||
|
#5
| |||
| |||
|
#6
| |||
| |||
|
|
person left join ( select marriage.marriage_id, marriage.marriage_date, marriage.husband_id, marriage.wife_id from marriage order by marriage.marriage_date ) blah_blah on (person.person_id = blah_blah.husband_id or person.person_id = blah_blah.wife_id) |
#7
| |||
| |||
|
|
person left join ( * *select * * * *husband_id as person_id * * *, marriage_date * * *, marriage_id * *from marriage * *union * *select * * * *wife_id as person_id * * *, marriage_date * * *, marriage_id * *from marriage * *order by person_id, marriage_date ) holy_union on person.person_id = holy_union.person_id |
![]() |
| Thread Tools | |
| Display Modes | |
| |