我想使用“金额”列和“客户编号”列总计每个客户已花费的金额。
String query = "SELECT customerNumber, SUM(amount) FROM payments";
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
orderNumbers.add(rs.getDouble(1));
System.out.println(rs.getDouble(1));
}
我已经在上面尝试过此代码,并且它只返回第一个customerNumber。
您需要使用group by
子句进行聚合:
SELECT customerNumber, SUM(amount)
FROM payments
GROUP BY customerNumber;