我想将透视列值的总和添加到新列并将输出记录插入到临时表中
select * from (select v.JobNo as JobNo,aj.VehicleNumber as VehicleNo,isnull(g.ImageCount,0) as ImageCount,s.ParamKey as ImageType from dbo.Visits v inner join (select VisitId as visit,paramkey,Value from dbo.VisitParams) s on s.visit = v.visitid left outer join ( select VisitId,FieldId, ( COUNT(*)) as ImageCount from dbo.vw_ImageGallery group by FieldId,VisitId) g on s.visit = g.VisitId and g.FieldId = s.ParamKey inner join Users u on u.UserId = v.CreatedBy inner join AssignedJobs aj on aj.CSRCode = u.Code and aj.JobNumber = v.JobNo where v.VisitType = 1 and v.TimeVisited >= '2019-03-01' AND v.TimeVisited <= '2019-04-01' )as a
PIVOT ( max([ImageCount]) FOR [ImageType] IN ([5],[20],[21]) ) as pvt order by [JobNo]
我的实际输出是
job no vehicleno 1 2 5
---------------------------------------------------------
BL1052385 648792 0 8 0
BL1054161 CAT2410 2 8 0
BL1107290 NB 0134 0 5 0
BL1174714 GP 3784 1 7 3
我希望输出类似
job no vehicleno 1 2 5 Total Count
----------------------------------------------------------
BL1052385 648792 0 8 0 8
BL1054161 CAT2410 2 8 0 10
BL1107290 NB 0134 0 5 0 5
BL1174714 GP 3784 1 7 3 11
答案 0 :(得分:0)
我更喜欢使用条件聚合而不是function factory() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var supSheet = ss.getSheetByName('Suppliers');
var supNames = supSheet.getRange(1, 1, supSheet.getLastRow(), 1).geValues();
supNames = supNames.map(function(el) {
return el[0];
});
supNames.forEach(function(name, i) {
doTheWork(ss, name, i, supNames);
});
}
function doTheWork(ss, name, i, allNames) {
if (!name || name.length == 0) return;
ss.getSheetByName('Data').activate();
var nSheet = ss.duplicateActiveSheet();
nSheet.setName(name);
nSheet.getRange('A:L').activate();
nSheet.getRange('A:L').createFilter();
nSheet.getRange('A1').activate();
var values = [''].concat(allNames);
values.splice(i+1, 1);
var criteria = SpreadsheetApp.newFilterCriteria()
.setHiddenValues(values)
.build();
nSheet.getFilter().setColumnFilterCriteria(1, criteria);
nSheet.getRange('G:G').activate();
nSheet.getRange('C1').activate();
nSheet.getFilter().sort(3, true);
nSheet.getRange('D1').activate();
nSheet.getFilter().sort(4, true);
}
。更加灵活:
pivot