sql - 当我的价格在 Apex Oracle SQL 的不同表中时,如何找到分组范围的平均价格?
问题描述
SCORE_T 表的图片 我正在尝试找到一种方法将我的平均价格编码到我从单独的表中建立的范围内。因此,我用来形成范围的点来自我的 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
;
谢谢!
解决方案
您需要 aJOIN
并且您正在使用avg()
no 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;
?
是连接乐谱和葡萄酒表的列的占位符。
推荐阅读
- mysql - MySQL Workbench 编辑器突出显示文本颜色
- java - 从 Java 项目生成 CSV 时更改文件名
- python - Keras初学者:最后一层的输出形状应该是什么?
- python - 类变量,instance.var 和 instance.__class__.var 的区别
- c# - unity,沿轴旋转对象并沿同一方向返回起点
- javascript - 如何在 javascript 中添加属性 runat="server"
- node.js - 删除 src 文件夹中的所有文件
- java - 我无法在 Maven 存储库中找到 Itext.text jar。你能给我提供下载jar的链接吗?
- python-3.x - Python Error No such table using sqlite3
- javascript - 如何在 Vue 中删除列表项时为列表项设置动画