日期:2014-05-18 浏览次数:20765 次
select a.creater, ltrim(datediff(mi,a.createtime,a.updatetime)/nullif(count(distinct b.pid),0))+'分钟' as [everyAct], ltrim(datediff(mi,a.createtime,a.updatetime)/nullif(count(distinct b.typeno),0))+'分钟' as [everyTct], px=row_number() over (order by datediff(mi,a.createtime,a.updatetime)/nullif(count(distinct b.pid),0)) from 主表 a join 明细表 b on a.id = b.billid group by a.creater,a.createtime,a.updatetime
------解决方案--------------------
declare @主表 table (BILLID int,CREATTIME datetime,UPDATETIME datetime,CREATER varchar(3)) insert into @主表 select 1001,'2011-09-11 09:00','2011-09-11 09:06','AAA' union all select 1002,'2011-09-11 09:00','2011-09-11 10:06','BBB' declare @明细表 table (BILLID int,PID varchar(5),TYPENO varchar(4)) insert into @明细表 select 1001,'P1001','UUIO' union all select 1001,'P1002','UUIO' union all select 1001,'P1003','YYXJ' union all select 1002,'P1001','X90X' union all select 1002,'P1002','ME90' select CREATER, 单张花费= ltrim(datediff(mi,a.CREATTIME,a.UPDATETIME)/b.c)+'分钟', 单个TYPENO花费= ltrim(datediff(mi,a.CREATTIME,a.UPDATETIME)/c.c)+'分钟' from @主表 a left join (select BILLID,count(1) as c from @明细表 group by BILLID ) b on a.BILLID=b.BILLID left join (select BILLID,count(distinct TYPENO) as c from @明细表 group by BILLID ) c on a.BILLID=c.BILLID /* CREATER 单张花费 单个TYPENO花费 ------- ---------------- ---------------- AAA 2分钟 3分钟 BBB 33分钟 33分钟 */
------解决方案--------------------
--主表 --BILLID CREATTIME UPDATETIME CREATER --1001 2011-09-11 09:00 2011-09-11 09:06 AAA --1002 2011-09-11 09:00 2011-09-11 10:06 BBB --明细表 --BILLID PID TYPENO --1001 P1001 UUIO --1001 P1002 UUIO --1001 P1003 YYXJ --1002 P1001 X90X --1002 P1002 ME90 create table matb(BILLID INT, CREATTIME VARCHAR(50), UPDATETIME VARCHAR(50),CREATER VARCHAR(50) ) INSERT INTO matb select 1001 ,'2011-09-11 09:00 ','2011-09-11 09:06',' AAA' union all select 1002,'2011-09-11 09:00', '2011-09-11 10:06', 'BBB' create TABLE mxtb ( BILLID int ,PID varchar(50),TYPENO VARCHAR(50) ) INSERT INTO mxtb select 1001, 'P1001', 'UUIO' union all select 1001, 'P1002', 'UUIO' union all select 1001, 'P1003', 'YYXJ' union all select 1002, 'P1001', 'X90X' union all select 1002, 'P1002', 'ME90' --要求计算每个人平均处理子单据的时间: --CREATER 单张花费 单个TYPENO花费 排名 -- AAA 2分钟 3分钟 1 -- BBB 33分钟 33分钟 2 select a.creater, ltrim(datediff(mi,a.CREATTIME,a.UPDATETIME)/nullif(count(distinct b.pid),0))+'分钟' as 单张花费, ltrim(datediff(mi,a.CREATTIME,a.UPDATETIME)/nullif(count(distinct b.typeno),0))+'分钟' as 单个TYPENO花费, row_number() over (order by datediff(mi,a.CREATTIME,a.UPDATETIME)/nullif(count(distinct b.pid),0)) AS 排名 from matb a join mxtb b on a.BILLID = b.BILLID group by a.creater,a.CREATTIME,a.UPDATETIME creater 单张花费 单个TYPENO花费 排名 -------------------------------------------------- ---------------- ---------------- -------------------- AAA 2分钟 3分钟 1 BBB 33分钟 33分钟 2 (2 行受影响)
------解决方案--------------------