一个有关分组查询,取每个分组的某个字段的最大值的那条记录的ID

时间:2023-01-29 18:04:21
这张表是卡使用记录表,现在要取出每张卡的最后产生(最后产生就是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



更糟糕的是该表中的纪录还有重复的,插入的时候重复插入某些记录。

9 个解决方案

#1



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))

#2


select * from table1 where id in(select max(id) from table1 group by cardno)

#3


select *  from ceshi a
where a.id in (select top 1 id from ceshi b
where b.cardno =a.cardno
order by b.todotime desc)

#4


谢谢一楼hinco!!

我想问一个问题:
我看有些语句会出现
SELECT 1 FROM XXXXX

那个1是什么意思呢?

#5


1楼正解

#6


引用楼主 huansinho 的帖子:
这张表是卡使用记录表,现在要取出每张卡的最后产生(最后产生就是todoTime时间最大的那条记录)的一条使用记录,但表中的记录不是顺序插入的而且时间可能是相同的,现在无法通过时用 GROUP BY cardNo 和 MAX(ID) 来获取的最后产生的那条记录。 


表中比较关键的字段: 

自动增长      卡号        产生时间  该卡使用的次数 
ID          cardNo      todoTime    useCount 
------------------------------------------ 



大哥,你那个1分是怎么个意思呢?

#7


select 1 from XXX where 
1 就是常量值,表示这个查询有返回结果,

#8


1就是常量,你也可以用2、3,或者select 字段 from...
这个没有什么特殊规定
主要是判断有没有数据用的。

#9



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

#1



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))

#2


select * from table1 where id in(select max(id) from table1 group by cardno)

#3


select *  from ceshi a
where a.id in (select top 1 id from ceshi b
where b.cardno =a.cardno
order by b.todotime desc)

#4


谢谢一楼hinco!!

我想问一个问题:
我看有些语句会出现
SELECT 1 FROM XXXXX

那个1是什么意思呢?

#5


1楼正解

#6


引用楼主 huansinho 的帖子:
这张表是卡使用记录表,现在要取出每张卡的最后产生(最后产生就是todoTime时间最大的那条记录)的一条使用记录,但表中的记录不是顺序插入的而且时间可能是相同的,现在无法通过时用 GROUP BY cardNo 和 MAX(ID) 来获取的最后产生的那条记录。 


表中比较关键的字段: 

自动增长      卡号        产生时间  该卡使用的次数 
ID          cardNo      todoTime    useCount 
------------------------------------------ 



大哥,你那个1分是怎么个意思呢?

#7


select 1 from XXX where 
1 就是常量值,表示这个查询有返回结果,

#8


1就是常量,你也可以用2、3,或者select 字段 from...
这个没有什么特殊规定
主要是判断有没有数据用的。

#9



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