我试图在多张 table 的情况下找出平均年龄。问题是年龄是从出生日期计算的派生属性。现在我写了下面的查询,其中包含所有满足条件的人的年龄。现在我想找到所有这些的平均年龄,但我不确定如何在 super 查询中使用子查询的结果。
查询是:
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));
输出是所有年龄的一列列表,你能告诉我如何计算平均年龄吗,因为这是我唯一想显示的东西。
最佳答案
如果我没理解错的话,您只需要一个简单的数字作为最终结果。您只需再上一级即可找到平均年龄。
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)))
关于mysql - 如何在 super 查询中使用子查询的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019493/