title: How to retrieve sum of a column of data from the database?
body: <p>How to retrieve sum of a column of data from the database and subract it from another column of data from the database? I want to do it like, get the sum from Total_Sales column and sum from Total_Value column and perform subract operation ie, subract value of Total_Value from value of Total_Sales (Total_Sales - Total_Value).  Currently this is my piece of code. </p>
  
  <pre><code>calculateButton.addActionListener(new ActionListener()
      {
      public void actionPerformed(ActionEvent ae)
      {
  
      try
      {
          Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
          Connection conn = DriverManager.getConnection("Jdbc:Odbc:TomsJava");
  
          java.util.Date fromDate=(java.util.Date)fromtextField.getValue();
          java.util.Date toDate=(java.util.Date)totextField.getValue();
  
          PreparedStatement ps = conn.prepareStatement("select SUM(Total_Sales - Total_Value) As Result from DailyAnalysis where 'Date1 BETWEEN #fromDate# AND #toDate#' ");
          ResultSet rs = ps.executeQuery();
          while(rs.next())
          {
  
              String itemName = rs.getString("Item");
              Double totalValue = rs.getDouble("Total_Value");
              Double totalSales = rs.getDouble("Total_Sales");
  
              Double Result = rs.getDouble(1);
  
          }
  
      }
      catch(SQLException s)
      {
          System.out.println(s);
      }
      catch(ClassNotFoundException z)
      {
          System.out.println(z);
      }
      catch(NumberFormatException n)
      {
          System.out.println(n);
      }
      }
      });
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/Ehv3b.jpg" alt="enter image description here"></p>
  
  <p><strong>EDITS</strong>
  I am trying to retrieve of the columns between two dates. I want to set the total of retrieved value of Total_Value to the Total Expense text field, and sum of Total_Sales column to the Total Income textfield. Then calculate the profit or loss based on that. </p>