我有一个名为ranks
的表,它存储1-7值。
r_id | r_userid | r_class | r_showcaseid | r_submitterid
r_class
是1-7值,r_showcaseid
是给定评级的项目。
以下查询选择用户前5个提交(r_showcaseid
)的平均评分。我需要获得行的平均值'此查询的值(即平均值的平均值):
SELECT avg(r_class) FROM ranks WHERE r_submitterid=? GROUP BY r_showcaseid ORDER BY avg(r_class) DESC LIMIT 5
所以,我想要的值是(6 + 5 + 1.5)/3≈4.166......
我试过avg(avg(r_class))
,但这给了我一个错误。如何从查询中获取行的avg(r_class)
的平均值?
答案 0 :(得分:3)
try
{
// Get Connection
Connection connection = SQLServer.getSQLServerConnection();
// Create statement
Statement statement = connection.createStatement();
String sql = " select distinct PositionTitle, max(NumberOFCount) as votes From votecount Group By PositionTitle order by votes desc";
//Execute SQL statement returns a ResultSet object.
ResultSet rs = statement.executeQuery(sql);
List<String> position =new ArrayList<>();
List<String> count =new ArrayList<>();
int i = 0;
while(rs.next() && i<6){
position.add(i, rs.getString(1));
count.add(i, rs.getString(2));
i++;
}
rs.close();
String[] pstn = (String[]) position.toArray(new String[position.size()]);
String[] cnt = (String[]) count.toArray(new String[count.size()]);
/* Iterator<String> postn = position.iterator();
Iterator<String> cnt = count.iterator(); */
for(int j=0;j<6;j++)
{
String sql1 = " Select FirstName from votecount where PositionTitle ='"+pstn[j]+"' and NumberOFCount = '"+cnt[j]+"'";
ResultSet rs1 = statement.executeQuery(sql1);
String FirstName = rs1.getString(0);
%>
<p align="center"><%=pstn[j] %> : <%=FirstName %> </p>
<p></p>
<%
}
}
catch (Exception e)
{
e.printStackTrace (response.getWriter());
}%>
您可以使用外部查询来执行此操作。