I am trying to find the average age in a scenario of multiple tables. The problem is that age is a derived attribute calculated from date of birth. Now I have written the following query which has the age of all the people that meet the condition. Now I want to find the average age of all of these but I am not sure how to use the results of the sub-query in the super-query.
The query is:
Select
case
when date(dob, '+' ||
(strftime('%Y', 'now') - strftime('%Y', dob)) ||
' years') <= date('now')
then strftime('%Y', 'now') - strftime('%Y', dob)
else strftime('%Y', 'now') - strftime('%Y', dob) - 1
end
as age
from UserProfile where User_ID in
(Select User_ID from UserProfile
where User_ID IN
(Select Channel_ID
from Channels
where Channel_Type = 'Public-Channel'
group by Channel_ID
HAVING (SUM(LENGTH(Video_IDs) - LENGTH(REPLACE(Video_IDs, ',', '')) + 1)) > 4));
And the output is a one column list of all ages, can you tell me how to calculate the average age because that's the only thing I want to display.
If I understand correctly, you just want one simple number as the end result. You can just go up one more level to find the average age.
SELECT avg(age) FROM (
Select
case
when date(dob, '+' ||
(strftime('%Y', 'now') - strftime('%Y', dob)) ||
' years') <= date('now')
then strftime('%Y', 'now') - strftime('%Y', dob)
else strftime('%Y', 'now') - strftime('%Y', dob) - 1
end
as age
from UserProfile where User_ID in
(Select User_ID from UserProfile
where User_ID IN
(Select Channel_ID
from Channels
where Channel_Type = 'Public-Channel'
group by Channel_ID
HAVING (SUM(LENGTH(Video_IDs) - LENGTH(REPLACE(Video_IDs, ',', '')) + 1)) > 4)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.