求高手赐SQL语句!!!!!!!!!!!!!

yifan1008 2007-09-28 04:10:10
create table #A(idno varchar(6),B varchar(1),icdate varchar(10),ictime varchar(8))
insert #A
select '000008',T,'2007/9/26','01:00:00' union all
select '000008',T,'2007/9/26','07:00:00' union all
select '000008',T,'2007/9/26','12:30:00' union all
select '000008',T,'2007/9/26','13:30:00' union all
select '000008',T,'2007/9/26','17:00:00' union all
select '000008',T,'2007/9/27','00:30:00' union all

select '000011',F,'2007/9/26','13:00:00' union all
select '000011',F,'2007/9/26','19:00:00' union all
select '000011',F,'2007/9/26','23:00:00' union all
select '000011',F,'2007/9/27','01:00:00' union all
select '000011',F,'2007/9/27','07:00:00' union all
select '000011',F,'2007/9/27','11:30:00' union all

select '000008',T,'2007/9/27','07:30:00' union all
select '000008',T,'2007/9/27','10:10:00' union all
select '000008',T,'2007/9/27','12:30:00' union all

select '000011',F,'2007/9/27','07:30:00' union all
select '000011',F,'2007/9/27','10:10:00' union all
select '000011',F,'2007/9/27','12:10:00' union all

要得到结果,B 字段的值是确定的。 他决定了 时间的处理方式。

设X为日期 T白班 (X的1:00(包含1:00)到X+1的00:30:00(包含X+1的00:30:00)) 跨度约23个半小时
设X为日期 F晚班 (X的13:00(包含13:00)到X+1的12:30:00(包含X+1的12:30:00)) 跨度约23个半小时

最后结果
odno icdate T1 T2 T3 T4 T5 T6 T7 T8 八个固定时间字段
000008 2007/9/26 01:00:00 07:00:00 12:30:00 13:30:00 17:00:00 00:30:00 NULL NULL
000011 2007/9/26 13:00:00 19:00:00 23:00:00 01:00:00 07:00:00 07:30:00 10:10:00 11:30:00
000008 2007/9/27 07:30:00 10:10:00 12:30:00 NULL NULL NULL NULL NULL


在结果中忽略 多于的就是 超出8个的时间 如结果第二条记录中就有
select '000011',F,'2007/9/27','12:10:00' union all 被忽略了。

希望大家一起帮帮忙。
...全文
272 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
tomyuansir 2007-10-06
  • 打赏
  • 举报
回复
学习啊 !!!!!!!!!!!!!
yifan1008 2007-10-01
  • 打赏
  • 举报
回复
整个原始数据中,000008 的B字段的值不是T就是F,是统一的。

也可以说不在处理数据的时候不用考虑B字段值,但在处理前B字段决定,了处理的时间段。谢谢!!!
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
也就是说你要的结果是
26 号 000008 的时间点范围范围是 (X >= 26号 01:00:00) and (X <= 27号 00:30:00)
26 号 000011 的时间点范围范围是 (X >= 26号 13:00:00) and (X <= 27号 12:30:00)
结果与F,T无关了
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
26 号 000008 的时间点范围范围是 (X >= 26号 01:00:00) and (X <= 27号 00:30:00)
26 号 000011 的时间点范围范围是 (X >= 26号 13:00:00) and (X <= 27号 12:30:00)
那么此时是否要考虑白班,晚班呢,
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
我在工作,无法上qq,可以skype:elvis_gao1,我没有看你的时间范围,只是把时间变成行,但是实现思路给你了,你加个条件,自己试试吧,我也是新手,我有时间再调调
yifan1008 2007-09-30
  • 打赏
  • 举报
回复
我需要的结果应该是
000008 2007/9/26 01:00:00 07:00:00 12:30:00 13:30:00 17:00:00 00:30:00 NULL NULL
000011 2007/9/26 13:00:00 19:00:00 23:00:00 01:00:00 07:00:00 07:30:00 10:10:00 11:30:00
000008 2007/9/27 07:30:00 10:10:00 12:30:00 NULL NULL NULL NULL NULL

设X为日期 000008的 B为 T (X的1:00(包含1:00)到X+1的00:30:00(包含X+1的00:30:00)) 跨度约23个半小时
设X为日期 000011的 B为 F (X的13:00(包含13:00)到X+1的12:30:00(包含X+1的12:30:00)) 跨度约23个半小时
{
你的结果
4 000011 2007/9/25 13:00:00 19:00:00 23:00:00 NULL NULL NULL NULL NULL
3 000011 2007/9/26 01:00:00 07:00:00 11:30:00 07:30:00 10:10:00 12:10:00 NULL NULL
2 000008 2007/9/26 01:00:00 07:00:00 12:30:00 13:30:00 17:00:00 01:00:00 07:00:00 12:30:00
1 000008 2007/9/27 00:30:00 07:30:00 10:10:00 12:30:00 NULL NULL NULL NULL
}
怎么会有25号的数据呢?

分析结果
26 号 000008 的时间点范围范围是 (X >= 26号 01:00:00) and (X<= 27号 00:30:00)
26 号 000011 的时间点范围范围是 (X >= 26号 13:00:00) and (X<= 27号 12:30:00)
取个自范围内的时间点,不足8个点的为null
说白了,就是,要更具条件范围把,时间日常(零点)跳日期的时间,推迟几个小时。

还有,你上面的SQL因该那里漏了什么了。
我是没有你那么厉害,不可能完成的了,你只是理解可能不对。帮帮忙。
有联系方式吗?
我 QQ号 16815517 谢谢!!!!!
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
哈哈,原来有这个东西,这回你粘上试试吧
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
drop table #A
drop table #tempTable
drop table #CountTable
drop table #T

create table #A(idno varchar(6),B varchar(1),icdate varchar(10),ictime varchar(8))
insert #A
select '000008','T','2007/9/26','01:00:00' union all
select '000008','T','2007/9/26','07:00:00' union all
select '000008','T','2007/9/26','12:30:00' union all
select '000008','T','2007/9/26','13:30:00' union all
select '000008','T','2007/9/26','17:00:00' union all
select '000008 ','T', '2007/9/26 ', '01:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '07:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '12:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '13:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '17:00:00 ' union all
select '000008 ','T', '2007/9/27 ', '00:30:00 ' union all
select '000011 ','F', '2007/9/26 ', '13:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '19:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '23:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '01:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '11:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '07:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '10:10:00 ' union all
select '000008 ','T', '2007/9/27 ', '12:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '10:10:00 ' union all
select '000011 ','F', '2007/9/27 ', '12:10:00 '

select *,identity(int,1,1) as id into #tempTable from #A
update #tempTable
set icdate=replace(convert(nvarchar(10), CONVERT(DateTime, icdate)-1,111),'/0','/')
where B='F'
select count(idno) as [count],idno,icdate,identity(int,1,1) as id into #CountTable from #tempTable group by idno,icdate Order by icdate desc
create table #T(id int, idno varchar(6),icdate varchar(10),T1 varchar(8),T2 varchar(8),T3 varchar(8),T4 varchar(8),T5 varchar(8),T6 varchar(8),T7 varchar(8),T8 varchar(8))
declare @i int
declare @str varchar(200)
set @i=(select count(*) from #CountTable)
while @i>0
begin
Select Top 1 * into #singleTable From #CountTable Where id in (Select Top (@i) id From #CountTable Order by id asc ) Order by id desc
declare @j int
declare @k int
declare @count int
declare @sql varchar(500)
declare @idno varchar(6)
declare @icdate varchar(10)

set @k=1
set @j=(select [count] from #singleTable)
set @count=(select [count] from #singleTable)
set @idno=(select idno from #singleTable)
set @icdate=(select icdate from #singleTable)
insert into #T(id,idno,icdate) values ((@i),@idno,@icdate)
while @j>0 and @k<=8
begin
set @sql = ' declare @T'+ convert(varchar(5),@k)+' varchar(10) set @T'+ convert(varchar(5),@k)+ ' = (Select Top 1 ictime From #tempTable Where id in (Select Top '+convert(varchar(5), @count-@j+1)+' id From #tempTable where icdate= (select icdate from #singleTable) and idno= (select idno from #singleTable) ) Order by id Desc) update #T set T' +convert(varchar(5),@k)+ '=@T' +convert(varchar(5),@k)+' where id=' +convert(varchar(5),@i)
exec(@sql)
set @j=@j-1
set @k=@k+1
end
drop table #SingleTable
set @i=@i-1
end
select * from #T
elvis_gao 2007-09-30
  • 打赏
  • 举报
回复
粘了之后的确有毛病,可能是我粘在贴里,再复制时,有很多多余的空格,我 给 你发了短消息,你粘上试试,我花了很长时间的,这是结果,不知符不符合你你的要求。
4 000011 2007/9/25 13:00:00 19:00:00 23:00:00 NULL NULL NULL NULL NULL
3 000011 2007/9/26 01:00:00 07:00:00 11:30:00 07:30:00 10:10:00 12:10:00 NULL NULL
2 000008 2007/9/26 01:00:00 07:00:00 12:30:00 13:30:00 17:00:00 01:00:00 07:00:00 12:30:00
1 000008 2007/9/27 00:30:00 07:30:00 10:10:00 12:30:00 NULL NULL NULL NULL
yifan1008 2007-09-30
  • 打赏
  • 举报
回复
elvis_gao


你把上面的全部复制到SQL运行器里,看看。
yifan1008 2007-09-30
  • 打赏
  • 举报
回复
000008 的B字段的结果是T是F,是很少变动的。处理时候也应当考虑T,F

000008 是T时候当天,时间点 是 (X >= 26号 01:00:00) and (X <= 27号 00:30:00)
000008 是F时候当天,时间点 是 (X >= 26号 13:00:00) and (X <= 27号 12:30:00)

你帮忙先给,做个,不用理会T还是F的,

也就是说你要的结果是
26 号 000008 的时间点范围范围是 (X >= 26号 01:00:00) and (X <= 27号 00:30:00)
26 号 000011 的时间点范围范围是 (X >= 26号 13:00:00) and (X <= 27号 12:30:00)
结果与F,T无关的。SQL
liubo1977 2007-09-29
  • 打赏
  • 举报
回复
select idno,max(case when ictime between '01:00:00' and '03:00:00' then ictime else null end) as T1,
max(case when ictime between '03:00:01' and '07:00:00' then ictime else null end) as T2,
max(case when ictime between '07:00:01' and '10:00:00' then ictime else null end) as T3,
max(case when ictime between '10:00:01' and '13:00:00' then ictime else null end) as T4,
max(case when ictime between '13:00:01' and '16:00:00' then ictime else null end) as T5,
max(case when ictime between '16:00:01' and '19:00:00' then ictime else null end) as T6,
max(case when ictime between '19:00:01' and '22:00:00' then ictime else null end) as T7,
max(case when ictime between '22:00:01' and '23:59:59' then ictime else null end) as T8
from #A
group by idno
elvis_gao 2007-09-29
  • 打赏
  • 举报
回复
drop table #A
drop table #tempTable
drop table #CountTable
drop table #T

create table #A(idno varchar(6),B varchar(1),icdate varchar(10),ictime varchar(8))
insert #A
select '000008','T','2007/9/26','01:00:00' union all
select '000008','T','2007/9/26','07:00:00' union all
select '000008','T','2007/9/26','12:30:00' union all
select '000008','T','2007/9/26','13:30:00' union all
select '000008','T','2007/9/26','17:00:00' union all
select '000008 ','T', '2007/9/26 ', '01:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '07:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '12:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '13:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '17:00:00 ' union all
select '000008 ','T', '2007/9/27 ', '00:30:00 ' union all
select '000011 ','F', '2007/9/26 ', '13:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '19:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '23:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '01:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '11:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '07:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '10:10:00 ' union all
select '000008 ','T', '2007/9/27 ', '12:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '10:10:00 ' union all
select '000011 ','F', '2007/9/27 ', '12:10:00 '

select *,identity(int,1,1) as id into #tempTable from #A
update #tempTable
set icdate=replace(convert(nvarchar(10), CONVERT(DateTime, icdate)-1,111),'/0','/')
where B='F'
select count(idno) as [count],idno,icdate,identity(int,1,1) as id into #CountTable from #tempTable group by idno,icdate Order by icdate desc
create table #T(id int, idno varchar(6),icdate varchar(10),T1 varchar(8),T2 varchar(8),T3 varchar(8),T4 varchar(8),T5 varchar(8),T6 varchar(8),T7 varchar(8),T8 varchar(8))
declare @i int
declare @str varchar(200)
set @i=(select count(*) from #CountTable)
while @i>0
begin
Select Top 1 * into #singleTable From #CountTable Where id in (Select Top (@i) id From #CountTable Order by id asc ) Order by id desc
declare @j int
declare @k int
declare @count int
declare @sql varchar(500)
declare @idno varchar(6)
declare @icdate varchar(10)

set @k=1
set @j=(select [count] from #singleTable)
set @count=(select [count] from #singleTable)
set @idno=(select idno from #singleTable)
set @icdate=(select icdate from #singleTable)
insert into #T(id,idno,icdate) values ((@i),@idno,@icdate)
while @j>0 and @k<=8
begin
set @sql = ' declare @T'+ convert(varchar(5),@k)+' varchar(10) set @T'+ convert(varchar(5),@k)+ ' = (Select Top 1 ictime From #tempTable Where id in (Select Top '+convert(varchar(5), @count-@j+1)+' id From #tempTable where icdate= (select icdate from #singleTable) and idno= (select idno from #singleTable) ) Order by id Desc) update #T set T' +convert(varchar(5),@k)+ '=@T' +convert(varchar(5),@k)+' where id=' +convert(varchar(5),@i)
exec(@sql)
set @j=@j-1
set @k=@k+1
end
drop table #SingleTable
set @i=@i-1
end
select * from #T
yifan1008 2007-09-29
  • 打赏
  • 举报
回复
drop table #A
drop table #tempTable
drop table #CountTable
drop table #T

create table #A(idno varchar(6),B varchar(1),icdate varchar(10),ictime varchar(8))
insert #A
select '000008 ', 'T ', '2007/9/26 ', '01:00:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '07:00:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '12:30:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '13:30:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '17:00:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '01:00:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '07:00:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '12:30:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '13:30:00 ' union all
select '000008 ', 'T ', '2007/9/26 ', '17:00:00 ' union all
select '000008 ', 'T ', '2007/9/27 ', '00:30:00 ' union all
select '000011 ', 'F ', '2007/9/26 ', '13:00:00 ' union all
select '000011 ', 'F ', '2007/9/26 ', '19:00:00 ' union all
select '000011 ', 'F ', '2007/9/26 ', '23:00:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '01:00:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '07:00:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '11:30:00 ' union all
select '000008 ', 'T ', '2007/9/27 ', '07:30:00 ' union all
select '000008 ', 'T ', '2007/9/27 ', '10:10:00 ' union all
select '000008 ', 'T ', '2007/9/27 ', '12:30:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '07:30:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '10:10:00 ' union all
select '000011 ', 'F ', '2007/9/27 ', '12:10:00 '

select *,identity(int,1,1) as id into #tempTable from #A
update #tempTable
set icdate=replace(convert(nvarchar(10), CONVERT(DateTime, icdate)-1,111), '/0 ', '/ ')
where B= 'F '
select count(idno) as [count],idno,icdate,identity(int,1,1) as id into #CountTable from #tempTable group by idno,icdate Order by icdate desc
create table #T(id int, idno varchar(6),icdate varchar(10),T1 varchar(8),T2 varchar(8),T3 varchar(8),T4 varchar(8),T5 varchar(8),T6 varchar(8),T7 varchar(8),T8 varchar(8))
declare @i int
declare @str varchar(200)
set @i=(select count(*) from #CountTable)
while @i >0
begin
Select Top 1 * into #singleTable From #CountTable Where id in (Select Top (@i) id From #CountTable Order by id asc ) Order by id desc
declare @j int
declare @k int
declare @count int
declare @sql varchar(500)
declare @idno varchar(6)
declare @icdate varchar(10)

set @k=1
set @j=(select [count] from #singleTable)
set @count=(select [count] from #singleTable)
set @idno=(select idno from #singleTable)
set @icdate=(select icdate from #singleTable)
insert into #T(id,idno,icdate) values ((@i),@idno,@icdate)
while @j >0 and @k <=8
begin
set @sql = ' declare @T '+ convert(varchar(5),@k)+ ' varchar(10) set @T '+ convert(varchar(5),@k)+ ' = (Select Top 1 ictime From #tempTable Where id in (Select Top '+convert(varchar(5), @count-@j+1)+ ' id From #tempTable where icdate= (select icdate from #singleTable) and idno= (select idno from #singleTable) ) Order by id Desc) update #T set T ' +convert(varchar(5),@k)+ '=@T ' +convert(varchar(5),@k)+ ' where id= ' +convert(varchar(5),@i)
exec(@sql)
set @j=@j-1
set @k=@k+1
end
drop table #SingleTable
set @i=@i-1
end
select * from #T


高手提示 LINE 43: TNCORRECT SYNTAX NEAR '('
yifan1008 2007-09-29
  • 打赏
  • 举报
回复
楼上的哥哥,提示43行有错 .
elvis_gao 2007-09-29
  • 打赏
  • 举报
回复
我花了好长时间做上的,比较麻烦呀
drop table #A
drop table #tempTable
drop table #CountTable
drop table #T

create table #A(idno varchar(6),B varchar(1),icdate varchar(10),ictime varchar(8))
insert #A
select '000008','T','2007/9/26','01:00:00' union all
select '000008','T','2007/9/26','07:00:00' union all
select '000008','T','2007/9/26','12:30:00' union all
select '000008','T','2007/9/26','13:30:00' union all
select '000008','T','2007/9/26','17:00:00' union all
select '000008 ','T', '2007/9/26 ', '01:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '07:00:00 ' union all
select '000008 ','T', '2007/9/26 ', '12:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '13:30:00 ' union all
select '000008 ','T', '2007/9/26 ', '17:00:00 ' union all
select '000008 ','T', '2007/9/27 ', '00:30:00 ' union all
select '000011 ','F', '2007/9/26 ', '13:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '19:00:00 ' union all
select '000011 ','F', '2007/9/26 ', '23:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '01:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:00:00 ' union all
select '000011 ','F', '2007/9/27 ', '11:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '07:30:00 ' union all
select '000008 ','T', '2007/9/27 ', '10:10:00 ' union all
select '000008 ','T', '2007/9/27 ', '12:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '07:30:00 ' union all
select '000011 ','F', '2007/9/27 ', '10:10:00 ' union all
select '000011 ','F', '2007/9/27 ', '12:10:00 '

select *,identity(int,1,1) as id into #tempTable from #A
update #tempTable
set icdate=replace(convert(nvarchar(10), CONVERT(DateTime, icdate)-1,111),'/0','/')
where B='F'
select count(idno) as [count],idno,icdate,identity(int,1,1) as id into #CountTable from #tempTable group by idno,icdate Order by icdate desc
create table #T(id int, idno varchar(6),icdate varchar(10),T1 varchar(8),T2 varchar(8),T3 varchar(8),T4 varchar(8),T5 varchar(8),T6 varchar(8),T7 varchar(8),T8 varchar(8))
declare @i int
declare @str varchar(200)
set @i=(select count(*) from #CountTable)
while @i>0
begin
Select Top 1 * into #singleTable From #CountTable Where id in (Select Top (@i) id From #CountTable Order by id asc ) Order by id desc
declare @j int
declare @k int
declare @count int
declare @sql varchar(500)
declare @idno varchar(6)
declare @icdate varchar(10)

set @k=1
set @j=(select [count] from #singleTable)
set @count=(select [count] from #singleTable)
set @idno=(select idno from #singleTable)
set @icdate=(select icdate from #singleTable)
insert into #T(id,idno,icdate) values ((@i),@idno,@icdate)
while @j>0 and @k<=8
begin
set @sql = ' declare @T'+ convert(varchar(5),@k)+' varchar(10) set @T'+ convert(varchar(5),@k)+ ' = (Select Top 1 ictime From #tempTable Where id in (Select Top '+convert(varchar(5), @count-@j+1)+' id From #tempTable where icdate= (select icdate from #singleTable) and idno= (select idno from #singleTable) ) Order by id Desc) update #T set T' +convert(varchar(5),@k)+ '=@T' +convert(varchar(5),@k)+' where id=' +convert(varchar(5),@i)
exec(@sql)
set @j=@j-1
set @k=@k+1
end
drop table #SingleTable
set @i=@i-1
end
select * from #T

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧