I've inherited a PostgreSQL 9.2.4 database and while I have a fairly extensive background in SQL Server I'm having a little trouble wrapping my head around a problem I'm encountering.
I have one table that has three fields (among other things) in it. "age_years", "age_months", and "age_days". If someone in the table is 2 months old or younger then they have a value in the "age_days" field for the number of days old they are. If they are less than 3 years old but older than 2 months then they have a value in the "age_months" field. Anything older than 3 and they have a value in the "age_years" field.
A given record only has a non-zero value in one of those three fields. There will never be a situation where, for instance, age_days and age_years both have a non-zero value. These records represent hospital visits and the ages are the age of the individual at the time of the visit.
In another table I have several character varying[] fields with up to 20 values. They are ref_age_cd, ref_age, ref_clow, and ref_chigh. Here is an example record from that table (with fewer values than the max just for display purposes):
My apologies for the ugly lines below. I can't seem to get them to format in a very readable condition.
ref_age_cd | ref_age | ref_clow | ref_chigh
[D,D,D,M,M,Y,Y,Y] [1,4,15,2,7,13,18,199] [9.1,9.8,5.4,5.5,7.9,5.1,4.8,4.8] [27.1,27.8,16.4,15.8,15.9,11.1,10.8,10.8]
The ref_age_cd field determines what kind of age you're looking at (days, months, or years). ref_age determines the value, and then based on those two you get the low and high values from the ref_clow and ref_chigh fields. So for example, if someone has a 13 in the age_months field then you would look at ref_age_cd and find the 'M' values in the array and then look at the corresponding ref_age field and find the largest value that is lower than the value in the age_months field. So the array index would be 5. Then you grab the fifth value in the ref_clow and ref_chigh fields for the low and high values. (7.9 and 15.9 respectively)
If someone was 10 days old the array index to look at would be 2 (ref_age_cd of 'D' and ref_age of 4). This would indicate a low and high value of 9.8 and 27.8. If they were 80 years old the index would be 7 (ref_age_cd of 'Y' and ref_age of 18). Low and high values of 4.8 and 10.8.
I just can't figure out how to program this so when I join from table A (with the age_days, age_months, or age_years fields) to the reference table I can pull the right array index for ref_clow and ref_chigh.
I should also mention that I have no ability to make any changes to this database. I need to make this work with what I've been given.
This ended up doing the trick. Posted so others might be able to use it.