Search code examples
sqlsumderby

Sum of column Sql error


I am busy creating a project and in this project I am required to get the number of diseases in a column. I attempted to use the sum function and followed tutorials, buuut It keeps on telling me that the column doesnt exist.

Connection con=   DriverManager.getConnection( "jdbc:derby://localhost:1527/MiniMed", "nbuser", "nbuser");
          ResultSet rs=con.createStatement().executeQuery("Select sum(Numcount) from Diseasetbl");

           System.out.println("Total Conditions Recorded: "+rs.getString("sum(Numcount)"));

Ive tried getting the sum of the conditions and it returns that it cannot use a varchar... therfore it knows that I DO HAVE Columns in the table. I cant figure out why this isnt working, any help would be appreciated, thank you. The table is called Diseasetbl and the column that I want to get the values from is an integer column and is called Numconditions.

java.sql.SQLException: There is no column named: sum(Numcount).


Solution

  • Try this,

    • Connection con= DriverManager.getConnection(jdbc:derby://localhost:1527/MiniMed", "nbuser", "nbuser");

    • ResultSet rs=con.createStatement().executeQuery("Select sum(Numcount) as quantity from Diseasetbl");

    • System.out.println("Total Conditions Recorded: "+rs.getString("quantity"));