下面的sql语句如何写?

jmqnwpu 2007-06-05 11:10:45
例子:
协议编号 企业编号 协议类型 有效期至
20060102005 xs002 计量 2006-05-08
20060709006 xs002 计量 2007-06-01
20060405001 xs002 食品 2007-06-02
20070805081 xs002 食品 2008-06-14
20060205099 dr008 计量 2006-09-08
20060915799 dr008 计量 2007-09-08
要求得到如下结果:
20060709006 xs002 计量 2007-06-01
20060405001 xs002 食品 2007-06-02
20060915099 dr008 计量 2006-09-08
即:
相同企业编号的并且相同协议类型的只列出最近的过期协议
...全文
198 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Andy-W 2007-06-05
  • 打赏
  • 举报
回复
没有必要考虑那么复杂,这样即可:

CREATE TABLE #T(协议编号 nvarchar(20),企业编号 nvarchar(20),协议类型 nvarchar(20),有效期至 smalldatetime)
INSERT INTO #T
SELECT '20060102005' ,'xs002' ,'计量' ,'2006-05-08' UNION ALL
SELECT '20060709006' ,'xs002' ,'计量' ,'2007-06-01' UNION ALL
SELECT '20060405001' ,'xs002' ,'食品' ,'2007-06-02' UNION ALL
SELECT '20070805081' ,'xs002' ,'食品' ,'2008-06-14' UNION ALL
SELECT '20060205099' ,'dr008' ,'计量' ,'2006-09-08' UNION ALL
SELECT '20060915799' ,'dr008' ,'计量' ,'2007-09-08'

SELECT * FROM #T AS A
WHERE NOT EXISTS(SELECT 1 FROM #T AS B WHERE B.企业编号=A.企业编号 AND B.协议类型=A.协议类型 AND
ABS(DATEDIFF(day,B.有效期至,GETDATE()))<ABS(DATEDIFF(day,A.有效期至,GETDATE())))
DROP TABLE #T
/*
20060709006 xs002 计量 2007-06-01
20060405001 xs002 食品 2007-06-02
20060915099 dr008 计量 2006-09-08
*/
fa_ge 2007-06-05
  • 打赏
  • 举报
回复
看到头顶了都是闪着,钻石,星星的
只接分好了
jinjazz 2007-06-05
  • 打赏
  • 举报
回复
折腾了半天发现有人写好了,不过漏了点,补上了,回复,发现已经补好了-_-|||
lwl0606 2007-06-05
  • 打赏
  • 举报
回复

select *
from table0 a
where 有效期至 < getdate() and
not exists (select 1 from table0 b where b.有效期至 < getdate() and a.企业编号=b.企业编号 and a.协议类型=b.协议类型 and a.有效期至<b.有效期至 )
子陌红尘 2007-06-05
  • 打赏
  • 举报
回复
折腾了半天,一开始想多了,接着又漏写了......
子陌红尘 2007-06-05
  • 打赏
  • 举报
回复
declare @t table(协议编号 varchar(12),企业编号 varchar(12),协议类型 varchar(12),有效期至 datetime)
insert into @t select '20060102005','xs002','计量','2006-05-08'
insert into @t select '20060709006','xs002','计量','2007-06-01'
insert into @t select '20060405001','xs002','食品','2007-06-02'
insert into @t select '20070805081','xs002','食品','2008-06-14'
insert into @t select '20060205099','dr008','计量','2006-09-08'
insert into @t select '20060915799','dr008','计量','2007-09-08'

select
t.*
from
@t t
where
t.有效期至<getdate()
and
not exists(select
1
from
@t
where
企业编号=t.企业编号
and
协议类型=t.协议类型
and
有效期至<getdate()
and
有效期至>t.有效期至)

/*
协议编号 企业编号 协议类型 有效期至
------------ ------------ ------------ ------------------------------------------------------
20060709006 xs002 计量 2007-06-01 00:00:00.000
20060405001 xs002 食品 2007-06-02 00:00:00.000
20060205099 dr008 计量 2006-09-08 00:00:00.000
*/
jinjazz 2007-06-05
  • 打赏
  • 举报
回复
select
t.*
from
表 t
where
t.有效期至<getdate()
and
not exists(select 1 from 表 where 企业编号=t.企业编号 and 协议类型=t.协议类型 and 有效期至>t.有效期至
)
子陌红尘 2007-06-05
  • 打赏
  • 举报
回复
declare @t table(协议编号 varchar(12),企业编号 varchar(12),协议类型 varchar(12),有效期至 datetime)
insert into @t select '20060102005','xs002','计量','2006-05-08'
insert into @t select '20060709006','xs002','计量','2007-06-01'
insert into @t select '20060405001','xs002','食品','2007-06-02'
insert into @t select '20070805081','xs002','食品','2008-06-14'
insert into @t select '20060205099','dr008','计量','2006-09-08'
insert into @t select '20060915799','dr008','计量','2007-09-08'

select
t.*
from
表 t
where
t.有效期至<getdate()
and
not exists(select 1 from 表 where 企业编号=t.企业编号 and 协议类型=t.协议类型 and 有效期至>t.有效期至)
子陌红尘 2007-06-05
  • 打赏
  • 举报
回复
select
t.*
from
表 t
where
t.有效期至<getdate()
and
not exists(select 1 from 表 where 企业编号=t.企业编号 and 协议类型=t.协议类型)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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