表1:drop table tb;create table tb(inspect_name varchar(20), vdatetime2 datetime, vtime int, jcnr varchar(20),rank varchar(2),uplj varchar(2));
insert into tb(inspect_name, vdatetime2, vtime,jcnr,rank,uplj)
select
'王','2010-01-03 00:33:52',60,'内容','A' ,'Y'union all select 
'王','2010-01-03 00:33:52',67,'内容','A' ,'Y'union all select 
'王','2010-01-03 12:32:50',100,'内容','B','Y' union all select
'王','2010-01-04 00:33:52',57,'内容','B' ,'Y'union all select 
'王','2010-01-04 00:33:52',62,'内容','B' ,'Y'union all select 
'王','2010-01-04 12:32:50',100,'内容','A','Y' union all select
'王','2010-01-09 00:33:52',60,'内容','B' ,'Y'union all select 
'王','2010-01-09 00:33:52',65,'内容','B' ,'Y'union all select 
'王','2010-01-09 12:32:50',130,'内容','B' ,'Y'union all select
'王','2010-02-13 00:33:52',60,'内容','B' ,'Y'union all select 
'王','2010-02-13 22:33:52',65,'内容','A' ,'Y'union all select 
'王','2010-02-13 12:32:50',100,'内容','A','Y';

表2 drop table jjj;create table jjj(aaa datetime);
insert into jjj(aaa)
select'2010-02-13 00:00:00';

用这条sql可以列出:
select Inspect_name,A,b, 节假日=SUM(节假日)
from (select Inspect_name, 
A=sum(A), B=sum(B),
节假日=sum(case when 节假日>119 then 1 else 0 end)
from (select Inspect_name, vdatetime2,aaa,
sum(case when rank= 'A' then 1 else 0 end) A,
sum(case when rank= 'B' then 1 else 0 end) B,
节假日= sum(case when vdatetime2=aaa then vtime else 0 end)from (select Inspect_name, rank,uplj,aaa,
(case when convert(varchar(16),vdatetime2,120) between convert(varchar(10),vdatetime2,120)+' 00:00' and convert(varchar(10),vdatetime2,120)+' 05:01' then convert(varchar(10),vdatetime2 -1,120)
else convert(varchar(10),vdatetime2,120) end) as vdatetime2,
vtime 
from tb,jjr where  vdatetime2  between '2010-1-2' and '2010-2-14') t group by  Inspect_name, vdatetime2,aaa )Tbl
group by Inspect_name,aaa ) k
group by Inspect_name,A,b

Inspect_name       A    B  节假日
王                 5    7    1
我现在还想再加一个表:
表3: drop table yyyy;create table yyyy(mmm varchar(20),xxx int,time datetime);
insert into yyyy(mmm,xxx,time)
select
'王',6,'2010-01-04 12:32:50'union all select 
'王',10,'2010-01-10 12:32:50';'王',100;

想要列出:如果我where选择时间范围是1月2日-2月14日
 Inspect_name      A    B  节假日  请假天数
王                 5    7    1     16
如果我where选择时间范围是1月2日-1月9日
 Inspect_name      A    B  节假日  请假天数
王                 3    3    1     6