22,209
社区成员
发帖
与我相关
我的任务
分享
select tid ,
tname ,
tdate ,
thour ,
newthour = case when datepart(weekday,tdate+@@datefirst-1) = 6
then (select min(thour) from tb where tname=a.tname and datepart(wk,tdate+@@datefirst-1) = datepart(wk,a.tdate+@@datefirst-1) and year(tdate)=year(a.tdate))
when thour = (select min(thour) from tb where tname=a.tname and datepart(wk,tdate+@@datefirst-1) = datepart(wk,a.tdate+@@datefirst-1) and year(tdate)=year(a.tdate)) and tdate= (select min(tdate) from tb where tname=a.tname and datepart(wk,tdate+@@datefirst-1) = datepart(wk,a.tdate+@@datefirst-1) and year(tdate)=year(a.tdate) and thour=((select min(thour) from tb where tname=a.tname and datepart(wk,tdate+@@datefirst-1) = datepart(wk,a.tdate+@@datefirst-1) and year(tdate)=year(a.tdate)) ) )
then (select thour from tb where tname=a.tname and datepart(weekday,tdate+@@datefirst-1) = 6 and datepart(wk,tdate+@@datefirst-1) = datepart(wk,a.tdate+@@datefirst-1) and year(tdate)=year(a.tdate) )
else thour
end
from tb a
declare @t table ([tid] int,[tname] varchar(10),[tdate] datetime,[thour] numeric(2,1))
insert into @t
select 1,'allen','2009-3-2',3.5 union all
select 2,'allen','2009-3-3',2 union all
select 3,'allen','2009-3-4',7 union all
select 4,'allen','2009-3-5',8 union all
select 5,'allen','2009-3-6',4 union all
select 6,'allen','2009-3-7',6 union all
select 7,'bill','2009-3-2',5 union all
select 8,'bill','2009-3-3',3 union all
select 9,'bill','2009-3-4',3 union all
select 10,'bill','2009-3-5',0.5 union all
select 11,'bill','2009-3-6',1 union all
select 12,'bill','2009-3-7',9
declare @sunday datetime
,@datefirst smallint
,@weekDays smallint
set @sunday = '1995-1-1'
set @weekDays = 7
set @datefirst = @@datefirst -1
select tid,tname,tdate
,(case
when (datepart(weekday,tdate+@datefirst)=6)
then min(thour)over(partition by tname,(datediff(day,@sunday,tdate)/@weekDays))
when ((thour=min(thour)over(partition by tname,(datediff(day,@sunday,tdate)/@weekDays)))
and (row_number()over(partition by tname,(datediff(day,@sunday,tdate)/@weekDays),thour order by tdate))=1)
then (sum(case when datepart(weekday,tdate+@datefirst)=6 then thour else 0 end)
over(partition by tname,(datediff(day,@sunday,tdate)/@weekDays)))
else thour
end ) as thour
from @t
order by tid
tid tname tdate thour
----------- ---------- ----------------------- ---------------------------------------
1 allen 2009-03-02 00:00:00.000 3.5
2 allen 2009-03-03 00:00:00.000 6.0
3 allen 2009-03-04 00:00:00.000 7.0
4 allen 2009-03-05 00:00:00.000 8.0
5 allen 2009-03-06 00:00:00.000 4.0
6 allen 2009-03-07 00:00:00.000 2.0
7 bill 2009-03-02 00:00:00.000 5.0
8 bill 2009-03-03 00:00:00.000 3.0
9 bill 2009-03-04 00:00:00.000 3.0
10 bill 2009-03-05 00:00:00.000 9.0
11 bill 2009-03-06 00:00:00.000 1.0
12 bill 2009-03-07 00:00:00.000 0.5
(12 行)
declare @t table ([tid] int,[tname] varchar(5),[tdate] datetime,[thour] numeric(2,1))
insert into @t
select 1,'allen','2009-3-2',1.5 union all
select 2,'allen','2009-3-3',2 union all
select 3,'allen','2009-3-4',7 union all
select 4,'allen','2009-3-5',8 union all
select 5,'allen','2009-3-6',4 union all
select 6,'allen','2009-3-7',6 union all
select 7,'bill','2009-3-2',5 union all
select 8,'bill','2009-3-3',3 union all
select 9,'bill','2009-3-4',3 union all
select 10,'bill','2009-3-5',8 union all
select 11,'bill','2009-3-6',8 union all
select 12,'bill','2009-3-7',9
declare @sunday datetime
set @sunday = '2009-3-1'
select tid,tname,tdate
,(case
when (datepart(weekday,tdate)=7)
then min(thour)over(partition by tname,(datediff(day,@sunday,tdate)/8))
when ((thour=min(thour)over(partition by tname,(datediff(day,@sunday,tdate)/78)))
and (row_number()over(partition by tname,(datediff(day,@sunday,tdate)/8),thour order by tdate))=1)
then (sum(case when datepart(weekday,tdate)=7 then thour else 0 end)
over(partition by tname,(datediff(day,@sunday,tdate)/8)))
else thour
end ) as thour
from @t
order by tid
tid tname tdate thour
----------- ----- ----------------------- ---------------------------------------
1 allen 2009-03-02 00:00:00.000 6.0
2 allen 2009-03-03 00:00:00.000 2.0
3 allen 2009-03-04 00:00:00.000 7.0
4 allen 2009-03-05 00:00:00.000 8.0
5 allen 2009-03-06 00:00:00.000 4.0
6 allen 2009-03-07 00:00:00.000 1.5
7 bill 2009-03-02 00:00:00.000 5.0
8 bill 2009-03-03 00:00:00.000 9.0
9 bill 2009-03-04 00:00:00.000 3.0
10 bill 2009-03-05 00:00:00.000 8.0
11 bill 2009-03-06 00:00:00.000 8.0
12 bill 2009-03-07 00:00:00.000 3.0
(12 行)
SELECT tname,tdate,
thour=CASE
WHEN datepart(w,tdate+@@DATEFIRST-1)=6
THEN (SELECT thour FROM tb WHERE t.tname=tname AND datepart(w,tdate+@@DATEFIRST-1)=1)
WHEN datepart(w,tdate+@@DATEFIRST-1)=1
THEN (SELECT thour FROM tb WHERE t.tname=tname AND datepart(w,tdate+@@DATEFIRST-1)=6)
ELSE thour
END
FROM tb t