We will be migrating from Ask to Discourse on the first week of August, read the details here

Ask Your Question
0

Age Query - Additional Columns [closed]

asked 2020-02-29 05:38:57 +0200

samueltmcf gravatar image

updated 2021-05-27 13:56:52 +0200

Alex Kemp gravatar image

I am using a bit of SQL from this thread: Calculate Age From Birthday to calculate the age in a query. It is working perfectly to calculate the age but I am having trouble adding additional columns to that query. I would like to at least add the "ID" column from the same table.

SELECT (1.0 * MYDATE / 365) AS AGE FROM (SELECT DATEDIFF( 'day', "BIRTHDATE", CURRENT_TIMESTAMP ) "MYDATE" FROM "BIRTHDATES")

In my basic knowledge of SQL I usually simply add a comma and the name of the next column that I would like to see in the query, but with this more complicated bit of code that includes parentheses, I haven't been able to figure out the correct syntax.

Thank you!

edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by Alex Kemp
close date 2021-05-27 13:56:44.538819

1 Answer

Sort by » oldest newest most voted
1

answered 2020-02-29 06:05:19 +0200

Ratslinger gravatar image

Hello,

See no reason for a sub query here. This works using HSQLDB:

SELECT "ID", 1.0 * DATEDIFF( 'day', "BIRTHDATE", CURRENT_TIMESTAMP ) / 365 As AGE FROM "BIRTHDATES"

Using your statement it would be:

SELECT "ID", (1.0 * MYDATE / 365) AS AGE FROM (SELECT "ID", DATEDIFF( 'day', "BIRTHDATE", CURRENT_TIMESTAMP ) "MYDATE" FROM "BIRTHDATES")
edit flag offensive delete link more

Comments

Yes, understood it was my statement in the comments. However things change in almost 4 years. :)

Ratslinger gravatar imageRatslinger ( 2020-02-29 06:40:46 +0200 )edit

@Ratslinger this worked perfectly. Thank you!

samueltmcf gravatar imagesamueltmcf ( 2020-02-29 15:57:21 +0200 )edit

Question Tools

1 follower

Stats

Asked: 2020-02-29 05:38:57 +0200

Seen: 95 times

Last updated: Feb 29 '20