用工作举个例子吧
比如说
job startmonth endmonth
1 1 4
1 1 5
1 2 3
1 7 9
1 8 11想得到job 1的经历时间
按照例子的话应该是
job sum_month
1 10
不知道说明白没有,请赐教
比如说
job startmonth endmonth
1 1 4
1 1 5
1 2 3
1 7 9
1 8 11想得到job 1的经历时间
按照例子的话应该是
job sum_month
1 10
不知道说明白没有,请赐教
from 表
where job=1
group by job
--但如果跨年就不对了。select job ,max(endmonth)-min(startmonth) as sum_month
from 表名
where job=1
group by job
因为6月,10月都是空闲的
=================================create table #temp1(job int,
startmonth int,
endmonth int
)insert into #temp1 select 1,1,4
union all select 1,1,5
union all select 1,2,3
union all select 1,7,9
union all select 1,8,11select * from #temp1select count(*) from (select DISTINCT startmonth from (select DISTINCT startmonth from #temp1 union all select DISTINCT endmonth from #temp1) a) b
drop table #temp1
改一下,这样好理解
insert @a select 1, 1 ,4
union all select 1, 1, 5
union all select 1, 2, 3
union all select 1, 7, 9
union all select 1, 8, 11
union all select 2, 8, 11select * into #tmp from @a a where not exists(select 1 from @a where job=a.job and st=a.st and en>a.en) and job=1
select job,sum(en+1)-sum(st) col from
(
select job,st,en from #tmp a where exists(select 1 from #tmp where job=a.job and st>a.st and en<a.en)
union
select a.job ,a.st,b.en from #tmp a,#tmp b where a.job=b.job and a.st<b.st and a.en>b.st and a.en<b.en
)aa
group by jobdrop table #tmp
一条sql估计不可以这个应该是表设计的问题,。,。
insert @a select 1, 1 ,4
union all select 1, 1, 5
union all select 1, 2, 3
union all select 1, 7, 9
union all select 1, 8, 11
union all select 1,23,45declare @b table(a int)select top 100 identity(int,0,1) id into #tmp from syscolumns
insert @b select b.id from #tmp b,@a a where b.id between st and en and job=1select distinct * into #g from @bselect identity(int,1,1) id1, * into #x from #g a where not exists(select 1 from #g where a.a=a+1) order by a
select identity(int,1,1) id2, * into #y from #g a where not exists(select 1 from #g where a.a+1=a) order by a
select sum(b.a+1)-sum(a.a) from #x a,#y b where a.id1=b.id2
select * from #ydrop table #tmp,#g,#x,#y
把包含关系的干掉,把交叉的提取两头,最好在 sum...
RETURNS int
AS
BEGIN
declare @min int,@max int,@thepty int,@minup int,@maxup int
select @min=0,@max=0,@minup=0,@maxup=0,@thepty=-1select @min=case when startmonth>@maxup and endmonth>@maxup then startmonth
else @minup end,
@max=case when startmonth>=@minup and endmonth>=@maxup then endmonth
when startmonth<=@max and endmonth>=@max then endmonth else @maxup end,
@thepty=@thepty+case when startmonth>@maxup and endmonth>@maxup then @maxup-@minup+1 else 0 end,
@minup=@min,@maxup=@max
from tt where job=@job order by startmonthreturn @thepty+@maxup-@minup+1
ENDdrop table tt
create table TT(job int, startmonth int, endmonth int )
insert into TT select 1, 1, 4
union all select 1, 1, 5
union all select 1, 2, 3
union all select 1, 7, 9
union all select 1, 8, 11
insert into TT select 2, 1, 4
union all select 2, 1, 3
union all select 2, 8, 11
select job, dbo.F_WWW(job)[monthcount] from tt group by job
job monthcount
----------- -----------
1 10 -----------1,2,3,4,5,7,8,9,10,11 (10)
2 8 -----------1,2,3,4,8,9,10,11 (8)
alter FUNCTION F_WWW(@job int)
RETURNS int
AS
BEGIN
declare @min int,@max int,@thepty int,@minup int,@maxup int
select @min=0,@max=0,@minup=0,@maxup=0,@thepty=-1select @min=case when startmonth>@maxup and endmonth>@maxup then startmonth else @minup end,
@max=case when startmonth<@maxup and endmonth<@maxup then @maxup else endmonth end,
@thepty=@thepty+case when startmonth>@maxup and endmonth>@maxup then @maxup-@minup+1 else 0 end,
@minup=@min,@maxup=@max
from tt where job=@job order by startmonthreturn @thepty+@maxup-@minup+1
END
select job,stuff('000000000000',st,en-st+1,replicate('1',en-st+1))A into #tab1 from 原表select job,0+max(substring(A,1,1))+max(substring(A,2,1))+max(substring(A,3,1))+max(substring(A,4,1))
+max(substring(A,5,1))+max(substring(A,6,1))+max(substring(A,7,1))+max(substring(A,8,1))
+max(substring(A,9,1))+max(substring(A,10,1))+max(substring(A,11,1))+max(substring(A,12,1))
from #tab1 group by job
stuff('000000000000',st,en-st+1,replicate('1',en-st+1))
换掉,再改一下表名即可,未考虑跨年度
select job,0+max(substring(A,1,1))+max(substring(A,2,1))+max(substring(A,3,1))+max(substring(A,4,1))
+max(substring(A,5,1))+max(substring(A,6,1))+max(substring(A,7,1))+max(substring(A,8,1))
+max(substring(A,9,1))+max(substring(A,10,1))+max(substring(A,11,1))+max(substring(A,12,1))
from(select job,stuff('000000000000',startmonth,endmonth-startmonth+1,
replicate('1',endmonth-startmonth+1))A from tt)a
group by job
select sum(datediff(m,endmonth,startmonth)) from job -----datetime
菜鸟曰:简单实用,上面的看不明白。
group by job
整理了一下 砍破 兄的方法跟大家分享下
wgzaaa() 和chuifengde(树上的鸟儿)的方法也可行,
不过似乎麻烦一些
多谢回帖的诸位code
CopyRight © w75251455(砍破)
No Right Reserved
--------------------
if exists (select [name] from sysobjects
where [name] = 'F_WWW'
)
drop function F_WWW
goCREATE FUNCTION F_WWW (@job int)
RETURNS int
AS
BEGIN
declare @min int, @max int, @thepty int, @minup int, @maxup int
select @min=0, @max=0, @minup=0, @maxup=0, @thepty=-1select @min = case
when startmonth > @maxup and endmonth > @maxup then startmonth
else @minup end
,@max = case
when startmonth >= @minup and endmonth >= @maxup then endmonth
when startmonth <= @maxup and endmonth >= @maxup then endmonth
else @maxup end
,@thepty = @thepty + case
when startmonth > @maxup and endmonth > @maxup then @maxup-@minup+1
else 0 end
,@minup = @min
,@maxup = @max
from tt
where job = @job
order by startmonthreturn @thepty + @maxup - @minup + 1
END
gocreate table TT
(job int, startmonth int, endmonth int )
insert into TT
select 1, 1, 4
union all
select 1, 1, 5
union all
select 1, 2, 3
union all
select 1, 7, 9
union all
select 1, 8, 11insert into TT
select 2, 1, 4
union all
select 2, 1, 3
union all
select 2, 8, 11select * from ttselect job, dbo.F_WWW(job) [monthcount] from tt group by jobdrop table tt