泰罗奥特曼之歌:sql server 简单的问题
来源:百度文库 编辑:高考问答 时间:2024/05/10 09:09:14
我有一个表table1 三个字段 uid date consume 分别表示会员ID, 消费时间,消费金额.
如:
uid date consume
1 2006-6-20 50
2 2006-7-5 90
3 2006-7-5 20
2 2006-7-20 120
1 2006-7-21 30
请问怎么用一个SQL语句查询出所有会员的最后一次消费记录呢
如:
uid date consume
1 2006-7-21 30
2 2006-7-20 120
3 2006-7-5 20
问题解决了再追回分啊.
已经自己解决了,不过有点小麻烦,不知道有没有人有更好的.
select a.* from table1 a join
(select uid,max(date)as date from table1 group by uid)b
on a.uid=b.uid and a.date=b.date
如:
uid date consume
1 2006-6-20 50
2 2006-7-5 90
3 2006-7-5 20
2 2006-7-20 120
1 2006-7-21 30
请问怎么用一个SQL语句查询出所有会员的最后一次消费记录呢
如:
uid date consume
1 2006-7-21 30
2 2006-7-20 120
3 2006-7-5 20
问题解决了再追回分啊.
已经自己解决了,不过有点小麻烦,不知道有没有人有更好的.
select a.* from table1 a join
(select uid,max(date)as date from table1 group by uid)b
on a.uid=b.uid and a.date=b.date
select a.* from table1 a join
(select uid,max(date)as date from table1 group by uid)b
on a.uid=b.uid and a.date=b.date
SELECT TOP 1 * FORM table1 ORDER BY date DESC
selece uid,date,consume
from table1,(数据库名)
group by uid
having max(date)
或者
selece top 1,uid,date,consume
from table1,(数据库名)
group by uid
orede by date desc
想不到简单的方法
不知道楼主用什么数据库
你可以用存储过程或程序的方法做
先选出 UID,MAX(DATE),用group by uid
然后依次确定相对应的consume
select 会员,(select top 1 consume from 表 b where a.会员=b.会员 order by date desc)consume from 表 a group by 会员