If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Auto Ahidjust Age

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-05-11, 21:58
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Auto Adjust Age

I don't know if this is a stupid question but I've been asked to create a contact list and there's a column requirement for the contacts dob DATE & age INT. My question is how would I be able to get the value for 'age' to adjust as time goes by based on their 'dob'? I've never done anything like this before using SQL statements.

Thanks for any help!

Last edited by CarlosinFL; 04-06-11 at 09:14.
Reply With Quote
  #2 (permalink)  
Old 04-06-11, 06:53
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
if the table contains only DOB, could you not define a view which calculates age on the fly?

if that doesn't satisfy whoever it is that "asked" you to have an age column, ask them to look up PHB in wikipedia

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 04-06-11, 09:06
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Quote:
Originally Posted by r937 View Post
if the table contains only DOB, could you not define a view which calculates age on the fly?
That was specifically my question. I looked online and searched here to see if anyone has ever "defined" a view which calculates age on the fly in the 'age' column based on the 'dob' column but I couldn't find any examples of how to formulate this in SQL. I'm assuming based on your response, that it can be. My question is how?

Quote:
Originally Posted by r937 View Post
]if that doesn't satisfy whoever it is that "asked" you to have an age column, ask them to look up PHB in wikipedia

I checked PHB in Wikipedia and unless we're referencing a Dilbert character or college degree, I have no idea what you're referencing here.

PHB - Wikipedia, the free encyclopedia
Reply With Quote
  #4 (permalink)  
Old 04-06-11, 09:53
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by CarlosinFL View Post
My question is how?
Code:
CREATE VIEW myview ( id, dob, age, ... )
AS
SELECT id, dob, YEAR(CURRENT_DATE)-YEAR(dob) AS age, ...
  FROM daTable
subtracting the years gives a close approximation of age, you can also use a more exact calculation

yes, PHB refers to the micro-managing idiot boss in Dilbert

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 04-06-11, 10:41
futurity futurity is offline
Registered User
 
Join Date: May 2008
Posts: 270
Why not just calculate it dynamically during your select?

Code:
select
    ...
    date_part('years', age(current_date, dob)) as age,
    ...
from some_table
where ....;
Reply With Quote
  #6 (permalink)  
Old 04-06-11, 10:49
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Quote:
Originally Posted by futurity View Post
Why not just calculate it dynamically during your select?

Code:
select
    ...
    date_part('years', age(current_date, dob)) as age,
    ...
from some_table
where ....;
I don't know but since 'age' is a column entry, when that SELECT statement is ran as:

Code:
SELECT * FROM users ORDER BY id;
wont the age parameters be wrong or invalid unless I run your suggested code?
Reply With Quote
  #7 (permalink)  
Old 04-06-11, 11:08
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by CarlosinFL View Post
... since 'age' is a column entry
we are suggesting that you ~not~ have an age column


whoa, wait a sec, postgresql has an AGE() function?

that's awesome! who knew?!!

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 04-06-11, 11:11
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Sorry - it wasn't clear to me that you suggest I omit the 'age' column in place if the AGE() function. It may seem elementary to you guys but I've only been using PG since 10/10 and am slowly learning everything by reading and self implementation. Thanks for the suggestion!

Code:
ALTER TABLE users DROP COLUMN age;
Reply With Quote
  #9 (permalink)  
Old 04-06-11, 11:24
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Quote:
Originally Posted by futurity View Post
Why not just calculate it dynamically during your select?

Code:
select
    ...
    date_part('years', age(current_date, dob)) as age,
    ...
from some_table
where ....;
I guess I don't quite follow this code based on my table parameters:

Code:
ide=# \d users
            Table "public.users"
 Column |         Type          | Modifiers 
--------+-----------------------+-----------
 id     | integer               | not null
 fname  | character varying(40) | not null
 lname  | character varying(40) | not null
 email  | character varying     | not null
 office | integer               | not null
 dob    | date                  | not null
Indexes:
    "users_pkey" PRIMARY KEY, btree (id)
Code:
ide=# SELECT * FROM users;
 id | fname  |  lname  |        email        | office |    dob     
----+--------+---------+---------------------+--------+------------
  1 | Carlos | Mennens | carlos@iamghost.org |    124 | 1980-05-12
(1 row)
When you suggest the code above, I'm having a hard time understanding the relation to my table.

I tried to relate the example but I'm missing something:

Code:
ide=# SELECT dob('years',age(current_date, dob)) as age WHERE id = 1;
ERROR:  column "dob" does not exist
LINE 1: SELECT dob('years', age(current_date, dob)) as age WHERE id =...
                                               ^
Reply With Quote
  #10 (permalink)  
Old 04-06-11, 11:31
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,538
Quote:
Originally Posted by CarlosinFL View Post
... but I'm missing something
yup, you're missing the FROM clause
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #11 (permalink)  
Old 04-06-11, 11:34
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Code:
ide=# SELECT extract('years' from age(CURRENT_TIMESTAMP,dob)) as age FROM users; 
 age 
-----
  31
(1 row)

ide=# SELECT age(dob) FROM users;
           age            
--------------------------
 31 years 10 mons 12 days
(1 row)
Thanks. Got too excited and jumped the gun!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On