19 个解决方案
#1
select StoreNum, max(T1) from test group by StoreNum
#2
select * from ( select * from test order by T1 desc) A group by A.StoreNum
#3
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum
AND A.T1<T1)
AND A.T1<T1)
#4
这个确实可以,不过如果最大值有多个相同,如何只取一个呢?并且如果表中有time这个字段,如何只取某段时间内的一个最大值呢?我现在写的这样的:
Select *
From TCtrl a where T1=
(Select distinct max(T1) From TCtrl where StoreNum=a.StoreNum) order by StoreNum,但是貌似还是不行
Select *
From TCtrl a where T1=
(Select distinct max(T1) From TCtrl where StoreNum=a.StoreNum) order by StoreNum,但是貌似还是不行
#5
+1
#6
如果再取某段时间内的记录,并且有多个相同最大值的情况下只取一条
#7
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
#8
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
表的字段有Id(主键),StoreNum,Time,T1...
#9
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
#10
有个Id字段作为主键,我实际情况是:取出最新的24小时内的记录,并按StoreNum分组,取出各组T1的最大值。当然可能存在多个相同最大值。
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
表的字段有Id(主键),StoreNum,Time,T1...
给表,给测试数据..
#11
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
为什么不用group by ,not exists效率高些吗
#12
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
#13
StoreNum T1 id上建立复合索引
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND (A.T1<T1 or (a.t1=t1 and a.id<id) )
and 时间 brtween .. and ...
)
and 时间 brtween .. and ...
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND (A.T1<T1 or (a.t1=t1 and a.id<id) )
and 时间 brtween .. and ...
)
and 时间 brtween .. and ...
#14
select * from ( select * from test where time between .. and .. order by T1 desc) A group by A.StoreNum
#15
select * from ( select * from test order by T1,time desc) A group by A.StoreNum
#17
select * from ( select * from test order by T1,time desc) A group by A.StoreNum
用这个,
select * from ( select * from test order by date desc, T1 desc) A group by A.StoreNum
#18
参考下贴中的多种方法
http://blog.csdn.net/acmain_chm/article/details/4126306
[征集]分组取最大N条记录方法征集,及散分....
from (select * from Table1 order by Score desc) t
group by ClsNo这种在SQL Server中不行,刚才试过了
#19
SQL SERVER 啊,这儿是MYSQL版,帮你转到SQL SERVER版咨询一下吧。
#20
#1
select StoreNum, max(T1) from test group by StoreNum
#2
select * from ( select * from test order by T1 desc) A group by A.StoreNum
#3
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum
AND A.T1<T1)
AND A.T1<T1)
#4
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum
AND A.T1<T1)
Select *
From TCtrl a where T1=
(Select distinct max(T1) From TCtrl where StoreNum=a.StoreNum) order by StoreNum,但是貌似还是不行
#5
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum
AND A.T1<T1)
+1
#6
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum
AND A.T1<T1)
+1
#7
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
#8
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
表的字段有Id(主键),StoreNum,Time,T1...
#9
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
#10
有个Id字段作为主键,我实际情况是:取出最新的24小时内的记录,并按StoreNum分组,取出各组T1的最大值。当然可能存在多个相同最大值。
并且有多个相同最大值的情况下只取一条:除非表中有唯一标识的字段
表的字段有Id(主键),StoreNum,Time,T1...
给表,给测试数据..
#11
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
为什么不用group by ,not exists效率高些吗
#12
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND
(A.T1<T1 or
(a.t1=t1 and a.id<id)
))
#13
StoreNum T1 id上建立复合索引
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND (A.T1<T1 or (a.t1=t1 and a.id<id) )
and 时间 brtween .. and ...
)
and 时间 brtween .. and ...
SELECT * FROM TT A WHERE NOT EXISTS(SELECT 1 FROM TT WHERE A.StoreNum=StoreNum AND (A.T1<T1 or (a.t1=t1 and a.id<id) )
and 时间 brtween .. and ...
)
and 时间 brtween .. and ...
#14
select * from ( select * from test where time between .. and .. order by T1 desc) A group by A.StoreNum
#15
select * from ( select * from test order by T1,time desc) A group by A.StoreNum
#16
#17
select * from ( select * from test order by T1,time desc) A group by A.StoreNum
用这个,
select * from ( select * from test order by date desc, T1 desc) A group by A.StoreNum
#18
参考下贴中的多种方法
http://blog.csdn.net/acmain_chm/article/details/4126306
[征集]分组取最大N条记录方法征集,及散分....
from (select * from Table1 order by Score desc) t
group by ClsNo这种在SQL Server中不行,刚才试过了
#19
SQL SERVER 啊,这儿是MYSQL版,帮你转到SQL SERVER版咨询一下吧。