我用sql2000,我用一个表A,表里有卡号(icid)计数器号(counter),消费时间等,我现在想查询列出每张卡

[复制链接]
查看11 | 回复4 | 2010-12-21 12:04:15 | 显示全部楼层 |阅读模式
我用sql2000,我用一个表A,表里有卡号(icid)计数器号(counter),消费时间等,我现在想查询列出每张卡的最大计数器号的那条消费记录

回复

使用道具 举报

千问 | 2010-12-21 12:04:15 | 显示全部楼层
这个肯定可以SELECT *FROM A tWHERE NOT EXISTS(SELECT 1 FROM A WHERE icid = t.icid AND counter > t.counter)
回复

使用道具 举报

千问 | 2010-12-21 12:04:15 | 显示全部楼层
select * from A join (select icid,max(counter) as acounter from A group by icid) as atabon A.icid=atab.icid and A.counter=atab.acounter差点忘了怎么写了...
回复

使用道具 举报

千问 | 2010-12-21 12:04:15 | 显示全部楼层
select * from a inner join (select icid,max(counter)as maxcountfrom A group by icid) b on a.icid =b.icid and a.counter = b.maxcount
回复

使用道具 举报

千问 | 2010-12-21 12:04:15 | 显示全部楼层
先取得每个卡的最大计数器号,再和原表做关联:select * from Aleft join (select max(counter),icid from A group by icid) Bon A.icid=B.icid and A.counter=B.counter
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行