How to retrieve sum of a column of data from the database?

前端 未结 1 1065
一整个雨季
一整个雨季 2021-01-28 00:01

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

相关标签:
1条回答
  • 2021-01-28 00:40
    Select Sum(Total_Sales) as Sales_Total, Sum(Total_Value) as Value_Total, Sum(Total_Sales - Total_Value) as Result
    From DailyAnalysis
    

    Change query and code to this

         Double result = null;
         Double totalValue = null;
         Double totalSales = null;
          ...
    
          PreparedStatement ps = conn.prepareStatement("select Sum(Total_Sales) as Sales_Total, Sum(Total_Value) as Value_Total, SUM(Total_Sales - Total_Value) As Result from DailyAnalysis where Date1 BETWEEN ? AND ? ");
                ps.setDate(1,fromDate)
                ps.setDate(2,toDate)
                ResultSet rs = ps.executeQuery();
    
                //Query should only return one or zero result hence the if statement
                if (rs.next())
                {  
                   //Define variables at the top, if you want to have access outside the if block scope
                   totalValue = rs.getDouble("Value_Total");
                   totalSales = rs.getDouble("Sales_Total");
                   result = rs.getDouble("Result");
                }
    
    0 讨论(0)
提交回复
热议问题