在SQL中对每个值求和

时间:2017-03-31 04:38:57

标签: sql sql-server sum

我有一个包含客户分类帐条目的表格,其中记录了每笔交易金额(包括费用和付款)。我想在查询中获得每个客户的余额,可能使用总和函数(余额的总和给出余额)。

我如何实现它?

以下是一些示例数据(我已经过滤了一个客户,虽然我有数千个)。

amount  Customer No.
-14,453.00  2709
-12,000.00  2709
-9,200.00   2709
-8,000.00   2709
-8,000.00   2709
-8,000.00   2709
-8,000.00   2709
-7,998.20   2709
-7,997.20   2709
-5,568.00   2709
-4,300.00   2709
-4,001.80   2709
-4,000.20   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-4,000.00   2709
-3,999.80   2709
-3,998.20   2709
-2,953.00   2709
-2,841.80   2709
-2,596.80   2709
-2,558.20   2709
-2,381.20   2709
-2,101.80   2709
-2,000.00   2709
-1,800.00   2709
-1,771.80   2709
-1,737.00   2709
-1,737.00   2709
-1,737.00   2709
-1,736.80   2709
-1,500.00   2709
-1,368.80   2709
-1,181.20   2709
-870    2709
-870    2709
-870    2709
-870    2709
-870    2709
-870    2709
-870    2709
-870    2709
-870    2709
-851.2  2709
-820    2709
-816.8  2709
-800    2709
-800    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-580    2709
-571.8  2709
-533.2  2709
-370    2709
-368.2  2709
-356.2  2709
-200    2709
-200    2709
-176.8  2709
-131.8  2709
-41.8   2709
-28.2   2709
-23.2   2709
-2.8    2709
-2.8    2709
-1.8    2709
-0.2    2709
1.8 2709
1.8 2709
2.8 2709
23.2    2709
131.8   2709
200 2709
200 2709
200 2709
368.2   2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
580 2709
800 2709
820 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
870 2709
1,160.00    2709
1,500.00    2709
1,737.00    2709
1,737.00    2709
1,737.00    2709
1,737.00    2709
1,737.00    2709
1,737.00    2709
1,800.00    2709
2,000.00    2709
2,101.80    2709
2,953.00    2709
2,953.00    2709
2,953.00    2709
2,953.00    2709
2,953.00    2709
2,953.00    2709
3,375.00    2709
3,375.00    2709
3,998.20    2709
4,000.00    2709
4,000.00    2709
4,000.00    2709
4,000.00    2709
4,000.00    2709
4,000.00    2709
4,000.00    2709
4,300.00    2709
5,568.00    2709
7,868.20    2709
7,976.80    2709
8,000.00    2709
8,000.00    2709
8,000.00    2709
8,000.00    2709
8,000.00    2709
8,000.00    2709
8,000.00    2709
9,200.00    2709
14,450.20   2709

2 个答案:

答案 0 :(得分:5)

只需使用const start = x => { // server dependencies const fs = require('fs'), express = require('express'), app = express(), router = express.Router(), bodyParser = require('body-parser'), api = require('./api'); const ramlConfig = { "server": { "notFoundHandler": false }, "disableErrorInterception": true } osprey.loadFile(cfg.API_SPEC, ramlConfig).then(_raml => { app.use(bodyParser.json({ extended: true })); // hide the useless "powered by express" header app.disable('x-powered-by'); // RAML validation app.use(cfg.API_MOUNT_POINT, _raml); app.use(customNotFoundHandler); app.use(ramlErrorChecker); app.use(cfg.API_MOUNT_POINT, api); //app.use(ramlErrorChecker); }) .then(v => { app.listen(cfg.SERVER_PORT, function() { log.info(startMessage); }); }) .catch(e => log.error(e)); } const ramlErrorChecker = (err, req, res, next) => { if (err) { log.error("RAML validation failed. Reason: " + err); res.status(400); return res.json(buildResponseJSON(400)); } return next(); } const customNotFoundHandler = (req, res, next) => { // Check for existence of the method handler. if (req.resourcePath) { return next() } res.status(404); return res.json({"message": "The path is not found"}); } AND SUM

即可
GROUP BY

答案 1 :(得分:0)

使用sumwhere

SELECT SUM(amount)
FROM table_name WHERE Customer_No=2709;