下面语句怎么写?谢谢!

tian790317 2005-11-13 03:07:41
"aid","bid","date1","exchage"
"a","b",2005-01-01 00:00:00,"0.1"
"a","c",2005-01-01 00:00:00,"0.2"
"a","d",2005-01-01 00:00:00,"0.3"
"a","e",2005-01-01 00:00:00,"0.5"
"b","a",2005-03-01 00:00:00,"0.5"
"b","c",2004-03-01 00:00:00,"0.6"
"b","d",2005-03-01 00:00:00,"0.8"
"c","a",2005-04-05 00:00:00,"0.1"
"c","b",2005-04-05 00:00:00,"0.5"
"c","d",2005-04-05 00:00:00,"0.2"
"c","e",2005-04-05 00:00:00,"0.2"
"e","a",2004-01-02 00:00:00,"0.1"
"e","h",2004-02-01 00:00:00,"0.5"
"a","b",2005-08-01 00:00:00,"0.1"
"a","c",2005-08-01 00:00:00,"0.2"
"a","d",2005-08-01 00:00:00,"0.3"
"a","e",2005-08-01 00:00:00,"0.5"
"c","b",2005-07-01 00:00:00,"0.1"
"c","a",2005-07-01 00:00:00,"01"
"c","d",2005-07-01 00:00:00,"0.4"
"c","e",2005-07-01 00:00:00,"0.4"
"d","a",2005-08-01 00:00:00,"0.5"
"d","b",2005-08-01 00:00:00,"0.8"
"d","c",2005-08-01 00:00:00,"0.4"
"d","e",2005-08-01 00:00:00,"0.5"
"d","a",2004-01-01 00:00:00,"0.3"
"d","c",2004-01-01 00:00:00,"0.5"
"d","c",2004-01-01 00:00:00,"0.6"
"d","a",2005-01-01 00:00:00,"0.1"
"d","c",2005-01-01 00:00:00,"014"
"d","b",2005-01-01 00:00:00,"0.4"
"d","e",2005-01-01 00:00:00,"0.6"
我想得到如下结果怎么做?也就是aid每天重复4次,并且date1是最新的日期的记录.谢谢!
"aid","bid","date1","exchage"
"a","b",2005-08-01 00:00:00,"0.1"
"a","c",2005-08-01 00:00:00,"0.2"
"a","d",2005-08-01 00:00:00,"0.3"
"a","e",2005-08-01 00:00:00,"0.5"
"c","b",2005-07-01 00:00:00,"0.1"
"c","a",2005-07-01 00:00:00,"01"
"c","d",2005-07-01 00:00:00,"0.4"
"c","e",2005-07-01 00:00:00,"0.4"
"d","a",2005-08-01 00:00:00,"0.5"
"d","b",2005-08-01 00:00:00,"0.8"
"d","c",2005-08-01 00:00:00,"0.4"
"d","e",2005-08-01 00:00:00,"0.5"
...全文
169 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lxzm1001 2005-12-26
  • 打赏
  • 举报
回复
create table #tb(aid varchar(1),bid varchar(1), date1 datetime, exchage numeric(8,1))
Insert into #tb (aid, bid, date1,exchage)
select 'a','b','2005-01-01 00:00:00','0.1'
union all select 'a','c','2005-01-01 00:00:00','0.2'
union all select 'a','d','2005-01-01 00:00:00','0.3'
union all select 'a','e','2005-01-01 00:00:00','0.5'
union all select 'b','a','2005-03-01 00:00:00','0.5'
union all select 'b','c','2004-03-01 00:00:00','0.6'
union all select 'b','d','2005-03-01 00:00:00','0.8'
union all select 'c','a','2005-04-05 00:00:00','0.1'
union all select 'c','b','2005-04-05 00:00:00','0.5'
union all select 'c','d','2005-04-05 00:00:00','0.2'
union all select 'c','e','2005-04-05 00:00:00','0.2'
union all select 'e','a','2004-01-02 00:00:00','0.1'
union all select 'e','h','2004-02-01 00:00:00','0.5'
union all select 'a','b','2005-08-01 00:00:00','0.1'
union all select 'a','c','2005-08-01 00:00:00','0.2'
union all select 'a','d','2005-08-01 00:00:00','0.3'
union all select 'a','e','2005-08-01 00:00:00','0.5'
union all select 'c','b','2005-07-01 00:00:00','0.1'
union all select 'c','a','2005-07-01 00:00:00','01'
union all select 'c','d','2005-07-01 00:00:00','0.4'
union all select 'c','e','2005-07-01 00:00:00','0.4'
union all select 'd','a','2005-08-01 00:00:00','0.5'
union all select 'd','b','2005-08-01 00:00:00','0.8'
union all select 'd','c','2005-08-01 00:00:00','0.4'
union all select 'd','e','2005-08-01 00:00:00','0.5'
union all select 'd','a','2004-01-01 00:00:00','0.3'
union all select 'd','c','2004-01-01 00:00:00','0.5'
union all select 'd','c','2004-01-01 00:00:00','0.6'
union all select 'd','a','2005-01-01 00:00:00','0.1'
union all select 'd','c','2005-01-01 00:00:00','014'
union all select 'd','b','2005-01-01 00:00:00','0.4'
union all select 'd','e','2005-01-01 00:00:00','0.6'

select * from #tb where
aid in (select aid from
(select aid,date1 ,count(aid) aa from #tb
where date1 in (select bb from (select aid,max(date1) bb from #tb group by aid) b) group by aid,date1 having count(aid)=4) a )
and date1 in (select bb from (select aid,max(date1) bb from #tb group by aid) b )
tian790317 2005-11-13
  • 打赏
  • 举报
回复
select * from test where aid in (select aid from (select aid,date1 ,count(aid) aa from test where date1 in (select bb from (select aid,max(date1) bb from test group by aid) b) group by aid,date1 having count(aid)=4) a ) and date1 in
(select bb from (select aid,max(date1) bb from test group by aid) b )
tian790317 2005-11-13
  • 打赏
  • 举报
回复
我自己已经写出来了:
select * from test where aid in (select aid from (select aid,date1 ,count(aid) aa from test group by aid,date1 having count(aid)=4) a ) and date1 in
(select bb from (select aid,max(date1) bb from test group by aid) b )
tian790317 2005-11-13
  • 打赏
  • 举报
回复
楼上的错了,不是我说的意思,仔细看看结果就知道题目的意思了
Andy__Huang 2005-11-13
  • 打赏
  • 举报
回复

create table #tb(aid varchar(1),bid varchar(1), date1 datetime, exchage numeric(8,1))
Insert into #tb (aid, bid, date1,exchage)
select 'a','b','2005-01-01 00:00:00','0.1'
union all select 'a','c','2005-01-01 00:00:00','0.2'
union all select 'a','d','2005-01-01 00:00:00','0.3'
union all select 'a','e','2005-01-01 00:00:00','0.5'
union all select 'b','a','2005-03-01 00:00:00','0.5'
union all select 'b','c','2004-03-01 00:00:00','0.6'
union all select 'b','d','2005-03-01 00:00:00','0.8'
union all select 'c','a','2005-04-05 00:00:00','0.1'
union all select 'c','b','2005-04-05 00:00:00','0.5'
union all select 'c','d','2005-04-05 00:00:00','0.2'
union all select 'c','e','2005-04-05 00:00:00','0.2'
union all select 'e','a','2004-01-02 00:00:00','0.1'
union all select 'e','h','2004-02-01 00:00:00','0.5'
union all select 'a','b','2005-08-01 00:00:00','0.1'
union all select 'a','c','2005-08-01 00:00:00','0.2'
union all select 'a','d','2005-08-01 00:00:00','0.3'
union all select 'a','e','2005-08-01 00:00:00','0.5'
union all select 'c','b','2005-07-01 00:00:00','0.1'
union all select 'c','a','2005-07-01 00:00:00','01'
union all select 'c','d','2005-07-01 00:00:00','0.4'
union all select 'c','e','2005-07-01 00:00:00','0.4'
union all select 'd','a','2005-08-01 00:00:00','0.5'
union all select 'd','b','2005-08-01 00:00:00','0.8'
union all select 'd','c','2005-08-01 00:00:00','0.4'
union all select 'd','e','2005-08-01 00:00:00','0.5'
union all select 'd','a','2004-01-01 00:00:00','0.3'
union all select 'd','c','2004-01-01 00:00:00','0.5'
union all select 'd','c','2004-01-01 00:00:00','0.6'
union all select 'd','a','2005-01-01 00:00:00','0.1'
union all select 'd','c','2005-01-01 00:00:00','014'
union all select 'd','b','2005-01-01 00:00:00','0.4'
union all select 'd','e','2005-01-01 00:00:00','0.6'

select * from #tb

select a.*
from #tb a
inner join (select aid,bid,date1=max(date1) from #tb group by aid,bid)b
on a.aid=b.aid and a.bid=b.bid and a.date1=b.date1
order by 1,2

aid bid date1 exchage
a b 2005-08-01 00:00:00.000 .1
a c 2005-08-01 00:00:00.000 .2
a d 2005-08-01 00:00:00.000 .3
a e 2005-08-01 00:00:00.000 .5
b a 2005-03-01 00:00:00.000 .5
b c 2004-03-01 00:00:00.000 .6
b d 2005-03-01 00:00:00.000 .8
c a 2005-07-01 00:00:00.000 1.0
c b 2005-07-01 00:00:00.000 .1
c d 2005-07-01 00:00:00.000 .4
c e 2005-07-01 00:00:00.000 .4
d a 2005-08-01 00:00:00.000 .5
d b 2005-08-01 00:00:00.000 .8
d c 2005-08-01 00:00:00.000 .4
d e 2005-08-01 00:00:00.000 .5
e a 2004-01-02 00:00:00.000 .1
e h 2004-02-01 00:00:00.000 .5



22,302

社区成员

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

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