Pic of SCORE_T table我正在尝试找到一种方法,将平均价格编码到我从单独的表格建立的范围内。因此,我过去用来形成范围的点来自POINTS_T表,并且使用出现的次数通过使用COUNT(*)建立一列。我现在正在尝试查找每个范围的平均价格,但是我的价格在WINE表中,因为我在使用子查询并且不知道在哪里包含它,所以遇到了麻烦。我已经附上了两张图片,其中一张是我的原始代码没有错误(因为我没有尝试包括平均价格),第二张包括了我尝试将其包含在子查询中的内容。
这是我到目前为止的代码(不包括我尝试的平均价格)。
SELECT t.range as "Score Range", COUNT(*) as "Number of Occurrences"
FROM (
SELECT Case
when SCORE_T.Points between 95 and 100 then '95-100'
when SCORE_T.Points between 90 and 94 then '90-94'
when SCORE_T.Points between 85 and 89 then '85-89'
when SCORE_T.Points between 80 and 84 then '80-84'
end as RANGE
FROM SCORE_T) t
GROUP BY t.range
ORDER BY COUNT(*) DESC
;
这就是我尝试的结果,这会产生错误。
SELECT t.range as "Score Range", COUNT(*) as "Number of Occurrences"
FROM (
SELECT Case
when SCORE_T.Points between 95 and 100 then '95-100'
when SCORE_T.Points between 90 and 94 then '90-94'
when SCORE_T.Points between 85 and 89 then '85-89'
when SCORE_T.Points between 80 and 84 then '80-84'
end as RANGE, avg(WINE.Price) as "Average Price of Wine"
FROM SCORE_T, WINE) t
GROUP BY t.range
ORDER BY COUNT(*) DESC
;
谢谢!
答案 0 :(得分:0)
您需要一个JOIN
,并且您正在使用avg()
,而没有GROUP BY
。我希望这样的事情:
SELECT sw.range as Score_Range,
COUNT(*) as Number_of_Occurrences,
AVG(sw.Price) as Average_Price
FROM (SELECT (Case when s.Points between 95 and 100 then '95-100'
when s.Points between 90 and 94 then '90-94'
when s.Points between 85 and 89 then '85-89'
when s.Points between 80 and 84 then '80-84'
end) as RANGE,
w.Price as Price
FROM SCORE_T s JOIN
WINE w
ON s.? = w.? -- what are the JOIN keys???
) sw
GROUP BY sw.range
ORDER BY COUNT(*) DESC;
?
是链接得分和酒水表的列的占位符。