一个计算天数的问题, 请帮帮忙怎么算??
现有一张出差表 User_Trip_Count, 内容就像这样:
Emp_ID FromDate FromTime ToDate ToTime
------ -------- -------- -------- ------
1 20030901 13:30 20030915 11:00
2 20030912 10:00 20030918 10:00
3 20030913 14:00 20030920 12:00
4 20030918 12:00 20030922 11:00
5 20030921 12:00 20030930 12:00
另有一张表 User_Trip_Days 保存出差天数, 现在要求把上面的数据导入
算法比较变态, 出发时间在 12:00 之后算半天, 回来时间在 12:00 之前也算半天
所以导入后的结果应该是
Emp_ID Days
------ ----
1 14
2 6.5
3 7.5
4 4.5
5 10
由于数据量非常大, 如果采用游标将花费很多时间, 请教这个SQL语句怎么写???
问题点数:0、回复次数:7Top
1 楼victorycyz(--)回复于 2003-11-04 10:11:12 得分 0
出发时间在12点之前怎么算?
回来时间在12点之后怎么算?Top
2 楼ken2002(尖刀)回复于 2003-11-04 10:26:49 得分 0
不知你的表里FromDate、FromTime、ToDate、ToTime是什么类型的呢Top
3 楼PCBoy008(PCBoy008)回复于 2003-11-04 10:30:53 得分 0
出发时间在 12:00 之前算 1 天
回来时间在 12:00 之后算 1 天
也就是老板的理想状态
Emp_ID FromDate FromTime ToDate ToTime
------ -------- -------- -------- ------
6 20030911 11:30 20030915 12:30
7 20030917 10:00 20030919 14:00
Emp_ID Days
------ ----
6 5
7 3
Top
4 楼zjcxc(邹建)回复于 2003-11-04 10:47:46 得分 0
insert into User_Trip_Days
select datediff(day,fromdate,todate)
+case when fromtime<='12:00' then 0.5 else 0 end
+case when totime>='12:00' then 0.5 else 0 end
from User_Trip_Count
Top
5 楼zjcxc(邹建)回复于 2003-11-04 10:48:38 得分 0
--上面少写了一个Emp_ID,改一下:
insert into User_Trip_Days
select Emp_ID,datediff(day,fromdate,todate)
+case when fromtime<='12:00' then 0.5 else 0 end
+case when totime>='12:00' then 0.5 else 0 end
from User_Trip_CountTop
6 楼zjcxc(邹建)回复于 2003-11-04 10:50:10 得分 0
--数据测试
select Emp_ID,days=datediff(day,fromdate,todate)
+case when fromtime<='12:00' then 0.5 else 0 end
+case when totime>='12:00' then 0.5 else 0 end
from(
select Emp_ID=1,FromDate='20030901',FromTime='13:30',ToDate='20030915',ToTime='11:00'
union all select 2,'20030912','10:00','20030918','10:00'
union all select 3,'20030913','14:00','20030920','12:00'
union all select 4,'20030918','12:00','20030922','11:00'
union all select 5,'20030921','12:00','20030930','12:00'
) a
/*--测试结果
Emp_ID days
----------- ---------------
1 14.0
2 6.5
3 7.5
4 4.5
5 10.0
(所影响的行数为 5 行)
--*/Top
7 楼wzh1215(懒猫)回复于 2003-11-04 10:59:42 得分 0
select EMP_ID,Days=(DateDiff(Day,convert(datetime,FromDate),convert(datetime,ToDate))-1)+(case when FromTime<'12:00' then 1 else 0.5 end)+case when ToTime>'12:00' then 1 else 0.5 end) from User_Trip_CountTop




