Ask表
AskID Question
1 问题1
2 问题2Reply表
ID Reply ReplyTime AskID
1 问题1 回答内容 2011/07/21 18:20:00 1
1 问题1 回答内容 2011/07/21 19:20:00 1
1 问题2 回答内容 2011/07/21 20:20:00 2现在想要去的内容是
AskID Question Reply ReplyTime
1 问题1 问题1 回答内容 2011/07/21 19:20:00
2 问题2 问题2 回答内容 2011/07/21 20:20:00Reply表中的信息有针对Ask表中问题1两次重复,想要结果中只取回答时间最晚的一条信息。
AskID Question
1 问题1
2 问题2Reply表
ID Reply ReplyTime AskID
1 问题1 回答内容 2011/07/21 18:20:00 1
1 问题1 回答内容 2011/07/21 19:20:00 1
1 问题2 回答内容 2011/07/21 20:20:00 2现在想要去的内容是
AskID Question Reply ReplyTime
1 问题1 问题1 回答内容 2011/07/21 19:20:00
2 问题2 问题2 回答内容 2011/07/21 20:20:00Reply表中的信息有针对Ask表中问题1两次重复,想要结果中只取回答时间最晚的一条信息。
解决方案 »
- 关于存储过程引号问题!!!
- 求教一条SQL语句
- 使用update trigger 时,造成死锁!
- 如何管理SDF的数据库文件(Sql Server Mobile)
- sql2008 链接服务器的时候出错,急求帮忙解决!!
- 谁有威盾数据库方面的资料
- 问题还是没有解决呀
- 几个字段的查询条件不同,如何合成一个数据集?
- 怎样察看sql server 2000的数据库日志
- JDBC 连接 sqlserver 错误:[Microsoft][SQLServer 2000 Driver for JDBC]Can't start a cloned connection while in manual transactio
- unique 和 primary key 不懂,请求教教我
- 求修改一个查询.
where a.AskID=b.AskID
and not exists(select 1 from Reply where AskID=b.AskID and ReplyTime>a.ReplyTime)
from ask a inner join Reply b on a.id=b.id
and not exists
(select 1 from Reply c where c.Reply=b.Reply and c.ReplyTime>b.ReplyTime)
from ask a inner join Reply b on a.AskID=b.id
and not exists
(select 1 from Reply c where c.Reply=b.Reply and c.ReplyTime>b.ReplyTime)
*
from
Ask a,Reply b
where
a.AskID=b.AskID
and
ReplyTime=(select max(ReplyTime) from Reply where AskID=b.AskID )
create table #Ask表
(AskID int, Question nvarchar(50))
insert #Ask表
select 1,'问题1' union all
select 2,'问题2'create table #Reply表
(ID int,Reply nvarchar(100), ReplyTime datetime, AskID int)
insert #Reply表
select 1,'问题1 回答内容', '2011/07/21 18:20:00', 1 union all
select 1,'问题1 回答内容', '2011/07/21 19:20:00', 1 union all
select 1,'问题2 回答内容', '2011/07/21 20:20:00', 2;with T as (select Row_number()over(partition by AskID order by ReplyTime desc) as num,
* from #Reply表)
select a.AskID,a.Question,r.Reply,r.ReplyTime from #Ask表 as a
join T as r on a.AskID=r.AskID where num=1--AskID Question Reply ReplyTime
------------- --------- --------------- -------------------------
--1 问题1 问题1 回答内容 2011-07-21 19:20:00.000
--2 问题2 问题2 回答内容 2011-07-21 20:20:00.000
--
--(2 row(s) affected)