具有不同列的总和查询
本文关键字:查询 | 更新日期: 2023-09-27 18:36:06
这是我的表批处理管理员,其中包含批处理名称,子批处理名称,记录等列
BatchName SubBatch records
12032014 raw1_0 2
12032014 raw1_0 2
12032014 raw1_1 2
12032014 raw1_1 2
12302014 raw7_0 150
12302014 raw7_0 150
我想获得具有不同批次名称,子批次和不同子批次的记录总和的输出。
BatchName SubBatch records
12032014 raw1_0 4
12032014 raw1_1 4
12302014 raw7_0 300
SELECT BatchName,SubBatch,SUM(records) AS Records
FROM urTableName
GROUP BY BatchName, SubBatch
这将
为你工作
SELECT BatchName,SubBatch,SUM(records) AS SumRecords
FROM batchmaster
GROUP BY BatchName, SubBatch
我同意Conrad Lotz的查询。
试试这个
DECLARE @batch_master TABLE
(
BatchName VARCHAR(50),
SubBatch VARCHAR(50),
records INT
)
INSERT INTO @batch_master
VALUES ('12032014',
'raw1_0',
'2'),
('12032014',
'raw1_0',
'2'),
('12032014',
'raw1_1',
'2'),
('12032014',
'raw1_1',
'2'),
('12302014',
'raw7_0',
'150'),
('12302014',
'raw7_0',
'150')
SELECT *
FROM @batch_master
SELECT batchname,
subbatch,
sum(records)
FROM @batch_master
GROUP BY batchname,
subbatch
尝试以下操作:
SELECT BatchName,SubBatch,SUM(records) AS Records
FROM Table
GROUP BY BatchName, SubBatch