问题
I'm making a JavaFX/SQL program representing Data between clients and clients spending. I am trying to calculate the total client spending by a specific client and output in the Javafx text area.
My Transactions table, has Date, Description, Amount, Clientname, and Transaction_ID where Transaction_ID is the primary key.
Here is my method "calculate_client_spending" "Result_transaction" is my global TextArea variable:
public void calculate_client_spending(){
try {
ConnectionClass Databaseloader = new ConnectionClass();
Databaseloader.getConnection();
String sql = "SELECT t.Amount, t.Clientname\n" +
" FROM Transactions AS t\n" +
" JOIN (SELECT Clientname, SUM(Amount) AS total\n" +
" FROM Transactions\n" +
" WHERE Clientname = 'Gaylord420') AS s ON t.Clientname =
s.Clientname";
ResultSet rs = Databaseloader.executeSQLRequestCommand(sql);
rs.next();
Results_trasactions.setText("");
Results_trasactions.setText("The total profits today are: " +
rs.getInt("total"));
}
}
catch (SQLException e) {
System.err.println("Got an exception! ");
System.err.println(e.getMessage());
}
}
The method method returns: "Got an exception! Column 'total' not found."
It seems to think "Total" is a column, but it is an alias, not an attribute in my "Transactions" Table. The total is supposed to the result of the final query. If it helps, my query was constructioned based on this post:
How to take sum of column with same id in SQL?
Any help would be great. Been stuck on this problem for a month now.
回答1:
You did not include the column total
in the select
list so in your code there are only 2 columns returned: Amount
and Clientname
.
Change to this:
String sql = "SELECT t.Amount, t.Clientname, s.total\n" +
...........................................................
来源:https://stackoverflow.com/questions/59523007/how-to-retrieve-the-total-variable-representing-a-sum-in-a-resultset