select * from 一个表 where 日期>getdate() group by 工号 having sum(小时)<=某个值
select * from table where 工号 in (select 工号 from table where sum(小时)<=timeValue group by 工号)
select * from t1 where num in (select num from t1 where dt1>getdate() group by num having sum(hou)<=6) t1:表 dt1:日期 num:工号 hou:小时
我觉得select * from t1 where num in (select num from t1 where dt1>getdate()不行,因为同一个工号有重复行,也许有大于或小于当前日期的 如果这样判断,那边where dt1>getdate()条件就没效果了(是不是这样)
我不是累计,而要要把符合条件的记录以列表的显示出来 以下我是使用游标,不知如何累加和,如下:DECLARE @COU NUMERIC SET @COU=0.0 DECLARE Employee_Cursor CURSOR FOR SELECT FILESNUM,YEARS,T1,T5 FROM paichong WHERE filesnum='w001'OPEN Employee_CursorWHILE @COU<20 BEGIN FETCH NEXT FROM Employee_Cursor SET @Cou=@Cou+5 /* 在此我想累加游标中T5的值如何写*/ END CLOSE Employee_Cursor DEALLOCATE Employee_Cursor另外,此游标显示在四个dbgrid中,我想返回在一个DBGrid中,如何实现
select * from tablename a where 日期>=convert(char(8),getdate(),112) and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=16
测试:create table tablename ( 工号 char(10), 日期 datetime, 小时 numeric(10,2) ) goinsert tablename select 'W001','2004-04-20',8 union all select 'W001','2004-04-21',8 union all select 'W001','2004-04-22',8 union all select 'W002','2004-04-20',8 union all select 'W002','2004-04-21',4 union all select 'W002','2004-04-22',8goselect * from tablename a where 日期>=convert(char(8),getdate(),112) and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=16结果: 工号 日期 小时 ---------- ------------------------------------------------------ ------------ W001 2004-04-21 00:00:00.000 8.00 W001 2004-04-22 00:00:00.000 8.00 W002 2004-04-21 00:00:00.000 4.00 W002 2004-04-22 00:00:00.000 8.00(所影响的行数为 4 行)select * from tablename a where 日期>=convert(char(8),getdate(),112) and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=12结果: 工号 日期 小时 ---------- ------------------------------------------------------ ------------ W001 2004-04-21 00:00:00.000 8.00 W002 2004-04-21 00:00:00.000 4.00 W002 2004-04-22 00:00:00.000 8.00(所影响的行数为 3 行) --删除测试表 drop table tablename
表A:
工号,日期,上午上班时间,上午下班时间,下午上班时间,下午下班时间,上午上班否,下午上班否,一天上班总共小时
例:W001,2004-04-20,8:30,12:30,13:30,17:30,true,true,8
W001,2004-04-21,8:30,12:30,13:30,17:30,true,true,8
W001,2004-04-22,8:30,12:30,13:30,17:30,true,true,8
我想求像上面的列表一样列出,但只要列到上班累积时间>设定的时间,就不应再列出
如:现在是2004-04-20
16小时,只显示1,2条记录,8小时,显示第1条记录,24小时,需要显示1,2,3条记录
50个小时,也是显示1,2,3条记录。(注,若以下有记录,再列,此只举三条记录)
where 日期>getdate()
group by 工号
having sum(小时)<=某个值
where dt1>getdate()
group by num
having sum(hou)<=6)
t1:表
dt1:日期
num:工号
hou:小时
where dt1>getdate()不行,因为同一个工号有重复行,也许有大于或小于当前日期的
如果这样判断,那边where dt1>getdate()条件就没效果了(是不是这样)
以下我是使用游标,不知如何累加和,如下:DECLARE @COU NUMERIC
SET @COU=0.0
DECLARE Employee_Cursor CURSOR FOR
SELECT FILESNUM,YEARS,T1,T5
FROM paichong
WHERE filesnum='w001'OPEN Employee_CursorWHILE @COU<20
BEGIN
FETCH NEXT FROM Employee_Cursor
SET @Cou=@Cou+5 /* 在此我想累加游标中T5的值如何写*/
END
CLOSE Employee_Cursor
DEALLOCATE Employee_Cursor另外,此游标显示在四个dbgrid中,我想返回在一个DBGrid中,如何实现
where 日期>=convert(char(8),getdate(),112)
and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=16
工号 char(10),
日期 datetime,
小时 numeric(10,2)
)
goinsert tablename
select
'W001','2004-04-20',8
union all select
'W001','2004-04-21',8
union all select
'W001','2004-04-22',8
union all select
'W002','2004-04-20',8
union all select
'W002','2004-04-21',4
union all select
'W002','2004-04-22',8goselect * from tablename a
where 日期>=convert(char(8),getdate(),112)
and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=16结果:
工号 日期 小时
---------- ------------------------------------------------------ ------------
W001 2004-04-21 00:00:00.000 8.00
W001 2004-04-22 00:00:00.000 8.00
W002 2004-04-21 00:00:00.000 4.00
W002 2004-04-22 00:00:00.000 8.00(所影响的行数为 4 行)select * from tablename a
where 日期>=convert(char(8),getdate(),112)
and (select sum(小时) from tablename where 工号=a.工号 and 日期>=convert(char(8),getdate(),112) and 日期<=a.日期)<=12结果:
工号 日期 小时
---------- ------------------------------------------------------ ------------
W001 2004-04-21 00:00:00.000 8.00
W002 2004-04-21 00:00:00.000 4.00
W002 2004-04-22 00:00:00.000 8.00(所影响的行数为 3 行)
--删除测试表
drop table tablename
为什么去掉判断日期的条件,这个语句的执行结果就会完全不同呢?