import java.io.*;
import java.sql.*;
public class Sum2{
public static void main(String[] args) {
System.out.println("Sum of the specific column!");
Connection con = null;
try
{
Class.forName("com.mysql.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/prathi","root","mysql");
try
{
Statement st = con.createStatement();
ResultSet res = st.executeQuery("SELECT Service_ID,SUM(consumer_feedback) FROM consumer2 group by Service_ID");
while (res.next())
{
int data=res.getInt(1);
System.out.println(data);
System.out.println("\n\n");
int c1 = res.getInt(2);
System.out.print(c1);
System.out.println("\n\n");
}
}
catch (SQLException s)
{
System.out.println("SQL statement is not executed!");
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
我的输出为
C:> javac Sum4.java
C:> java Sum4 具体栏目的总和! 31
0 0
32
2 2
33
-1 0
我计算了总和。然后我将负值分配为零。我需要对这些值进行归一化,即我必须找到2/2的值。 (首先我会找到总数并将每个值除以总数)。我希望自动模拟任何数字的结果。请给我一些想法
谢谢......
答案 0 :(得分:0)
这是你的意思吗?
SELECT Service_ID,SUM(consumer_feedback),Sum(consumer_feedback)/count(*)
FROM consumer2
GROUP BY Service_ID