这张表是卡使用记录表,现在要取出每张卡的最后产生(最后产生就是todoTime时间最大的那条记录)的一条使用记录,但表中的记录不是顺序插入的而且时间可能是相同的,现在无法通过时用 GROUP BY cardNo 和 MAX(ID) 来获取的最后产生的那条记录。
表中比较关键的字段:自动增长 卡号 产生时间 该卡使用的次数
ID cardNo todoTime useCount
------------------------------------------
1 001 2008-1-1 1
2 002 2008-1-1 1
3 001 2008-1-2 3
4 003 2008-1-4 2
5 004 2008-1-3 1
6 003 2008-1-1 1
7 002 2008-1-2 2
8 001 2008-1-1 2更糟糕的是该表中的纪录还有重复的,插入的时候重复插入某些记录。
表中比较关键的字段:自动增长 卡号 产生时间 该卡使用的次数
ID cardNo todoTime useCount
------------------------------------------
1 001 2008-1-1 1
2 002 2008-1-1 1
3 001 2008-1-2 3
4 003 2008-1-4 2
5 004 2008-1-3 1
6 003 2008-1-1 1
7 002 2008-1-2 2
8 001 2008-1-1 2更糟糕的是该表中的纪录还有重复的,插入的时候重复插入某些记录。
解决方案 »
- 请教一个基础问题主键和unique的区别
- 这段UPDATE语句有什么错误?
- 多用户插入数据,返回与库里重复的记录??
- 无法运行SQL SERVER 2005 配置服务器 老是出现WMI错误
- MSDE安装路径
- "相似性重复纪录检测"算法国内外的研究情况
- SQL Server2000升级到2005的疑问
- 对于表中记录查找相同记录,速度非常慢!
- 求助BCP导入 文本文件到数据库的 报错,请各位大侠看看/
- 创建bookDB数据库,在bookDB数据库中创建读者表readers、图书表books和图书借阅表borrow三个表,之后使用T_SQL语句完成如下操作。
- 关于死锁 和 事务捕捉的问题
- 系统存储过程:sp_datatype_info执行时间时长时短
select * from 表 a where not exists(select 1 from 表 where cardNo =a.cardNo and (a.todoTime<todoTime or a.todoTime=todoTime and a.ID<ID))
where a.id in (select top 1 id from ceshi b
where b.cardno =a.cardno
order by b.todotime desc)
我看有些语句会出现
SELECT 1 FROM XXXXX
那个1是什么意思呢?
1 就是常量值,表示这个查询有返回结果,
这个没有什么特殊规定
主要是判断有没有数据用的。
create table tb
(
ID int identity(1,1),
cardNo char(10),
todoTime datetime,
useCount int
)
insert into tb
select '001','2008-1-2',1 union all
select '001','2008-1-1',1 union all
select '002','2008-1-1',1 union all
select '001','2008-1-2',3 union all
select '003','2008-1-4',2 union all
select '004','2008-1-3',1 union all
select '003','2008-1-1',1 union all
select '002','2008-1-2',2 union all
select '001','2008-1-1',2
----------------------------------------------
select * from tb A
where not exists
(
select 1 from tb
where A.cardNo=tb.cardNo and
(A.todoTime=tb.todoTime and A.ID<tb.ID or A.todoTime<tb.todoTime)
)
-----------------------------------------------
4 001 2008-01-02 00:00:00.000 3
5 003 2008-01-04 00:00:00.000 2
6 004 2008-01-03 00:00:00.000 1
8 002 2008-01-02 00:00:00.000 2