[英]How to use results of a sub-query in a super query?
我試圖在多個表的情況下找到平均年齡。 問題在於,年齡是從出生日期算起的派生屬性。 現在,我寫了以下查詢,其中列出了所有符合條件的人的年齡。 現在,我想找到所有這些對象的平均年齡,但是我不確定如何在超級查詢中使用子查詢的結果。
查詢是:
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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.