求一SQL语句

骑牛上铂金 2009-05-01 11:49:05

aa bb cc
01 2009-1-1 400
01 2009-1-4 500
01 2009-1-29 900
02 2008-12-5 4000
02 2008-12-29 300
02 2009-1-1 5000
02 2009-2-1 450
...

结果(要求显示出bb值间隔不超过3天的所有记录)
如下:
aa bb cc
01 2009-1-1 400
01 2009-1-4 500
02 2008-12-29 300
02 2009-1-1 5000


请帮忙解决,不甚感激!

...全文
59 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
htl258_Tony 2009-05-02
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb]([aa] varchar(10),[bb] datetime,[cc] int)
insert [tb] select '01','2009-1-1',400
union all select '01','2009-1-3',500
union all select '01','2009-1-4',500
union all select '01','2009-1-29',900
union all select '02','2008-12-5',4000
union all select '02','2008-12-29',300
union all select '02','2009-1-1',5000
union all select '02','2009-2-1',450

select distinct a.*
from tb a
join tb b
on a.aa=b.aa
and a.bb<>b.bb
where abs(datediff(dd,a.bb,b.bb))<=3
/*
aa bb cc
---------- ----------------------- -----------
01 2009-01-01 00:00:00.000 400
01 2009-01-03 00:00:00.000 500
01 2009-01-04 00:00:00.000 500
02 2008-12-29 00:00:00.000 300
02 2009-01-01 00:00:00.000 5000

(5 行受影响)
*/
再加入一个日期调期。小改一下。
htl258_Tony 2009-05-02
  • 打赏
  • 举报
回复
try:
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb]([aa] varchar(10),[bb] datetime,[cc] int)
insert [tb] select '01','2009-1-1',400
union all select '01','2009-1-4',500
union all select '01','2009-1-29',900
union all select '02','2008-12-5',4000
union all select '02','2008-12-29',300
union all select '02','2009-1-1',5000
union all select '02','2009-2-1',450

select a.*
from tb a
join tb b
on a.aa=b.aa
and a.bb<>b.bb
where abs(datediff(dd,a.bb,b.bb))<=3
/*
aa bb cc
---------- ----------------------- -----------
01 2009-01-01 00:00:00.000 400
01 2009-01-04 00:00:00.000 500
02 2008-12-29 00:00:00.000 300
02 2009-01-01 00:00:00.000 5000

(4 行受影响)
*/
骑牛上铂金 2009-05-02
  • 打赏
  • 举报
回复
谢谢!
ws_hgo 2009-05-02
  • 打赏
  • 举报
回复
加个abs函数-->取绝对值的
select * from tb t where exists (select * from tb where aa=t.aa and abs(datediff(day,aa.tt.aa))<=3)
ws_hgo 2009-05-02
  • 打赏
  • 举报
回复
[Quote=引用楼主 n29882942 的帖子:]

aa bb cc
01 2009-1-1 400
01 2009-1-4 500
01 2009-1-29 900
02 2008-12-5 4000
02 2008-12-29 300
02 2009-1-1 5000
02 2009-2-1 450
结果(要求显示出bb值间隔不超过3天的所有记录)
如下:
aa bb cc
01 2009-1-1 400
01 2009-1-4 500
02 2008-12-29 300
02 2009-1-1 5000
[/Quote]
select * from tb t where exists (select * from tb where aa=t.aa and datediff(day,aa.tt.aa)<=3)
firefly_2008 2009-05-02
  • 打赏
  • 举报
回复
LS 正解

列队顶
htl258_Tony 2009-05-02
  • 打赏
  • 举报
回复
if object_id('[tb]') is not null drop table [tb] 
go
create table [tb]([aa] varchar(10),[bb] datetime,[cc] int)
insert [tb] select '01','2009-1-1',400
union all select '01','2009-1-3',500
union all select '01','2009-1-4',500
union all select '01','2009-1-29',900
union all select '02','2008-12-5',4000
union all select '02','2008-12-29',300
union all select '02','2009-1-1',5000
union all select '02','2009-2-1',450

select *
from tb t
where exists
(select 1 from tb where t.aa=aa
and t.bb<>bb and abs(datediff(dd,t.bb,bb))<=3)

/*
aa bb cc
---------- ----------------------- -----------
01 2009-01-01 00:00:00.000 400
01 2009-01-03 00:00:00.000 500
01 2009-01-04 00:00:00.000 500
02 2008-12-29 00:00:00.000 300
02 2009-01-01 00:00:00.000 5000

(5 行受影响)
*/
也可以用exists。
Stephen_Kang 2009-05-02
  • 打赏
  • 举报
回复
其实不用那么麻烦的。。。。、

只需。。获取bb列。。用函数。截取他们的 “日”(月/日/年) 用截取的两个“日”相减 where 条件就是。他们的值小于等于3

22,207

社区成员

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

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