tags:

views:

50

answers:

2
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();
   }

  }
} 

I got the output as

C:>javac Sum4.java

C:>java Sum4 Sum of the specific column! 31

0 0

32

2 2

33

-1 0

I calculated the sum .Then I assigned negative values as zero.I need to normalize these values ie I have to find the value of 2/2. (First I will find the total and divide each value by total). I want the result to be simulated automatically for any number.Please give me some idea

Thank You ...

A: 

Is this what you mean?

SELECT Service_ID,SUM(consumer_feedback),Sum(consumer_feedback)/count(*)
 FROM consumer2
 GROUP BY Service_ID
wallyk
A: 

Thank you Dmckee.I need to extract some data from the database.For example I extracted consumer_feedback (which consists of values 1,0,-1......)ie I found the sum of consumer_feedback.I got the result(After calculating the sum as 0,2,-1.As per my project I want to assign the negative values to zero.So now the result becomes 0,2,0.I have got the result.After that I want to normalize(for this I need ur help)ie I want to get the result as 0/0+2+0=0; 2/0+2+0=1; 0/0+2+0=0; Please give me some idea..Thanks a lot....

PRATHIBA