项目中碰到一个统计问题:生产派工单需要按名称进行分组,每一组的“标准工时”和“实际工时”需要进行按组统计。统计后的数据再进行分页显示。截图如下
UI图:
数据表结构图:
为了实现效果 模拟数据如下:
create table Test ( ID int identity(1,1) primary key not null, ProductName varchar(20), PassCount int, DisPassCount int, CraftHour numeric(18,2), StartDate datetime, EndDate datetime ) --插入数据 insert Test values('name1',10,1,1,'2001-1-1','2001-1-2') insert Test values('name2',20,20,2,'2001-1-2','2001-1-3') insert Test values('name2',20,20,2,'2001-1-4','2001-1-5') insert Test values('name3',30,3,1,'2001-1-11','2001-1-12') insert Test values('name4',13,3,1,'2001-1-12','2001-1-13') insert Test values('name5',3,3,1,'2001-1-14','2001-1-15') insert Test values('name6',3,3,1,'2001-1-14','2001-1-15') insert Test values('name7',3,3,1,'2001-1-14','2001-1-15') insert Test values('name8',3,3,1,'2001-1-14','2001-1-15') insert Test values('name9',3,3,1,'2001-1-14','2001-1-15') insert Test values('name10',3,3,1,'2001-1-14','2001-1-15') insert Test values('name11',3,3,1,'2001-1-14','2001-1-15') insert Test values('name12',3,3,1,'2001-1-14','2001-1-15') insert Test values('name13',3,3,1,'2001-1-14','2001-1-15') insert Test values('name14',3,3,1,'2001-1-14','2001-1-15') insert Test values('name15',3,3,1,'2001-1-14','2001-1-15') insert Test values('name16',3,3,1,'2001-1-14','2001-1-15') insert Test values('name17',3,3,1,'2001-1-14','2001-1-15') --分页前 select ProductName as '产品名称',sum((PassCount+DisPassCount)*CraftHour) as '标准工时',sum(DATEDIFF(hour,StartDate,EndDate)) as '实际工时' from Test group by ProductName order by ProductName desc --分页后(每一页5条记录) select top 5 ProductName,sum((PassCount+DisPassCount)*CraftHour),sum(DATEDIFF(hour,StartDate,EndDate)) from Test group by ProductName,ID having ProductName not in (select top (0*5) ProductName from Test group by ProductName order by ProductName desc) order by ProductName desc --分组后的记录行数 select COUNT(*) from (select distinct ProductName from Test) as a
模拟数据截图:
应该还有更好的做法,希望你能提点建议,不胜感谢。