http://www.cnblogs.com/isun/archive/2012/12/22/2829528.html
你要的,都在这里了
wo我去看看,非常感谢
你给的连接中下面的写法,完美解决了我的问题。非常感谢!
select * from
(
select *, ROW_NUMBER() over(partition by SectionId order by TransactionDate desc) as rowNum
from SectionTransactionLog
) ranked
where ranked.rowNum <= 2
order by ranked.SectionId, ranked.TransactionDate desc
@我叫So: 结帖,给分(^_^)
select UserId,min(Value) from T group by UserId
z这样写满足不了我的需求,而且表内还有其他列要返回,我只是为了简单只列了两个列
每个只要第一条?
e嗯,也就是每个UserId的第1/N条,也包括其他很多字段,我只是为了简单只列了两个列
百度:sql 分组取第一
y已解决了,非常感谢。
可以,使用row_number over()
select * from
(
select *, ROW_NUMBER() over(partition by SectionId order by TransactionDate desc) as rowNum
from SectionTransactionLog
) ranked
where ranked.rowNum <= 2
order by ranked.SectionId, ranked.TransactionDate desc
虽然结贴,但我看着答案还是很复杂,比较简单的方法是:
select max(userid),min(value) from T group by userid;
select userid,min(value_) from tt group by userid order by userid;