| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1151 人关注过本帖
标题:[求助]此类查询语句该如何写(有难度的题目)
只看楼主 加入收藏
weihongkun
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-12-8
收藏
得分:0 

求人回答,再顶上去

2006-12-24 19:21
LouisXIV
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:789
专家分:0
注 册:2006-1-5
收藏
得分:0 

declare @test table
(
BOM int identity(1,1),
T_ITEM char(4),
BEGIN_DATE datetime,
END_DATE datetime
)
insert into @test
select '2001','2006-08-29','2006-09-03' union all
select '2002','2006-08-30','2006-09-02' union all
select '2001','2006-09-03','2006-09-10' union all
select '2001','2006-09-12','2006-09-18' union all
select '2003','2006-08-29','2006-09-18' union all
select '2004','2006-09-29','2006-10-03' union all
select '2001','2006-09-18','2006-10-03' union all
select '2001','2006-10-10','2006-11-01'

select * from @test t
where BEGIN_DATE!=(select END_DATE from @test where T_ITEM=t.T_ITEM and BOM=(select max(BOM) from @test where T_ITEM=t.T_ITEM and BOM<t.BOM))
or END_DATE!=(select BEGIN_DATE from @test where T_ITEM=t.T_ITEM and BOM=(select min(BOM) from @test where T_ITEM=t.T_ITEM and BOM>t.BOM))


/*
BOM T_ITEM BEGIN_DATE END_DATE
----------- ------ ------------------------------------------------------ ------------------------------------------------------
3 2001 2006-09-03 00:00:00.000 2006-09-10 00:00:00.000
4 2001 2006-09-12 00:00:00.000 2006-09-18 00:00:00.000
7 2001 2006-09-18 00:00:00.000 2006-10-03 00:00:00.000
8 2001 2006-10-10 00:00:00.000 2006-11-01 00:00:00.000
*/


2006-12-24 23:05
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
收藏
得分:0 

我用存储过程做了一下,不过真麻烦
还是斑竹的简单
drop table s
create table s
(
T_ITEM int,
BEGIN_DATE datetime,
END_DATE datetime
)
insert s select 2001,'2006-08-29','2006-09-03' union all
select 2002,'2006-08-30','2006-09-02' union all
select 2001,'2006-09-03','2006-09-10' union all
select 2001,'2006-09-12','2006-09-18' union all
select 2003,'2006-08-29','2006-09-18' union all
select 2004,'2006-09-29','2006-10-03' union all
select 2001,'2006-09-18','2006-10-03' union all
select 2001,'2006-10-10','2006-11-01'
drop procedure pro_s
创建存储过程
create procedure pro_s
(@i int)
as
declare @j int
set @j=1
select identity(int,1,1) as bom ,* into #s1 from s where T_ITEM=@i
declare @begin_time datetime
declare @end_time datetime
while @j<=(select count(1) from #s1)-1
begin
select @end_time=end_date from #s1 where bom=@j
select @begin_time=begin_date from #s1 where bom=@j+1
if @begin_time<>@end_time
begin
print ' not filled'
select * from #s1 where T_ITEM=@i
set @j=(select count(1) from #s1)-1
end
set @j=@j+1
end
用临时表来保存T_ITEM
drop table #s
create table #s
(
i int identity(1,1),
T_ITEM int
)
insert into #s select distinct T_ITEM from s
用循环调用存储过程
declare @j int
declare @T_ITEM int
set @j=1
while @j<=(select count(1) from #s )
begin
select @T_ITEM=T_ITEM from #s where @j=i
exec pro_s @T_ITEM
set @j=@j+1
end

[此贴子已经被作者于2006-12-25 20:57:50编辑过]


26403021 sql群 博客 blog./user15/81152/index.shtml
2006-12-25 19:12
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
收藏
得分:0 
看了很长时间,才看懂斑竹的代码,但是有一点不明,如果插入的日期不是按从小到达的话,那段代码可以吗

26403021 sql群 博客 blog./user15/81152/index.shtml
2006-12-25 20:20
weihongkun
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-12-8
收藏
得分:0 

老大们加点注释呀?看不明白哦!

2006-12-29 21:16
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
收藏
得分:0 
以下是引用LouisXIV在2006-12-24 23:05:45的发言:

declare @test table
(
BOM int identity(1,1),
T_ITEM char(4),
BEGIN_DATE datetime,
END_DATE datetime
)
insert into @test
select '2001','2006-08-29','2006-09-03' union all
select '2002','2006-08-30','2006-09-02' union all
select '2001','2006-09-03','2006-09-10' union all
select '2001','2006-09-12','2006-09-18' union all
select '2003','2006-08-29','2006-09-18' union all
select '2004','2006-09-29','2006-10-03' union all
select '2001','2006-09-18','2006-10-03' union all
select '2001','2006-10-10','2006-11-01'

select * from @test t
where BEGIN_DATE!=(select END_DATE from @test where T_ITEM=t.T_ITEM and BOM=(select max(BOM) from @test where T_ITEM=t.T_ITEM and BOM<t.BOM))
or END_DATE!=(select BEGIN_DATE from @test where T_ITEM=t.T_ITEM and BOM=(select min(BOM) from @test where T_ITEM=t.T_ITEM and BOM>t.BOM))


/*
BOM T_ITEM BEGIN_DATE END_DATE
----------- ------ ------------------------------------------------------ ------------------------------------------------------
3 2001 2006-09-03 00:00:00.000 2006-09-10 00:00:00.000
4 2001 2006-09-12 00:00:00.000 2006-09-18 00:00:00.000
7 2001 2006-09-18 00:00:00.000 2006-10-03 00:00:00.000
8 2001 2006-10-10 00:00:00.000 2006-11-01 00:00:00.000
*/

我老大给你的这段程序你看懂了没啊


26403021 sql群 博客 blog./user15/81152/index.shtml
2006-12-29 21:35
weihongkun
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-12-8
收藏
得分:0 
还没完全弄懂,正在研究,谢了,大哥们
2006-12-30 14:49
棉花糖ONE
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:32
帖 子:2987
专家分:0
注 册:2006-7-13
收藏
得分:0 
先去找点简单的子查询看看,再来看他这代码

26403021 sql群 博客 blog./user15/81152/index.shtml
2006-12-30 14:53
weihongkun
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-12-8
收藏
得分:0 

问题基本得到解决,谢谢大家!
不过还没有完全弄明白原理,呵呵

2006-12-30 23:57
快速回复:[求助]此类查询语句该如何写(有难度的题目)
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.035492 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved