SQL Server:总结一列

时间:2017-04-20 11:46:54

标签: sql sql-server

我已尽力将Current列添加到其中。 'BF','Current'和'Future'都应该等于零,我只需要快速检查即可验证它。

代码

SELECT 
    [AccountCode], [ExpensesCode], [CostCentre],
    [NLYear], [NLPeriod],
    SUM([BroughtForward]) AS 'BF',
    SUM([CurrentPostings]) AS 'Current',
    SUM([FuturePostings]) AS 'Future',
    [Company]
FROM 
    [A_DW].[dbo].[NEW_ETL AccBalance]
WHERE
    [Company] = 'GAR' 
    AND NLPeriod = '3' AND NLYear = '2017'
GROUP BY
    [AccountCode], [ExpensesCode], [CostCentre], [NLYear], [NLPeriod], [Company]

这是Autoline数据库,数据是试算平衡,所以如下所示:

+-----+------+-----+------+---+---------+---------+------+    
| 100 | 8000 | 700 | 2017 | 3 | 1000.00 | 2000.00 | 0.00 |
| 100 | 8001 | 700 | 2017 | 3 | 1500.00 | 4500.00 | 0.00 |
+-----+------+-----+------+---+---------+---------+------+

结果应为6500.00(2000.00 + 4500.00)

2 个答案:

答案 0 :(得分:1)

根据示例数据,您的列ExpenseCode中包含多个不同的值。如果要将这两行折叠在一起,则需要决定如何处理该列。这是一个选择:忽略它!

SELECT 
    [AccountCode],
    --[ExpensesCode], --comment it out, or delete entirely
    [CostCentre],
    [NLYear], [NLPeriod],
    SUM([BroughtForward]) AS 'BF',
    SUM([CurrentPostings]) AS 'Current',
    SUM([FuturePostings]) AS 'Future',
    [Company]
FROM 
    [A_DW].[dbo].[NEW_ETL AccBalance]
WHERE
    [Company] = 'GAR' 
    AND NLPeriod = '3' AND NLYear = '2017'
GROUP BY
    [AccountCode],
    --[ExpensesCode], -- also comment out here
    [CostCentre],
    [NLYear],
    [NLPeriod],
    [Company]

答案 1 :(得分:0)

您获得的行数等于: [AccountCode],[ExpensesCode],[CostCentre],[NLYear],[NLPeriod]

的不同组合数量

如果您需要单行,只需从查询中删除这些列并保留总和列。

查询可以是:

app.get('/download', function(req, res){
  var file = __dirname + '/write/imageout.png';
  res.download(file);
});

希望这会有所帮助。