22,207
社区成员
发帖
与我相关
我的任务
分享
select top 1 * from (select COUNT(1) as cn,MAX([ctime]) as ct from #tab group by [ctime]) as t order by cn desc,ct desc
use Tempdb
go
--> --> 听雨停了-->测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([ctime] Datetime,[value] decimal(18,7))
Insert #tab
select '2017-09-11 09:34:00',23 union all
select '2017-09-11 09:34:00',220 union all
select '2017-09-11 09:34:00',109 union all
select '2017-09-11 09:36:00',28.9 union all
select '2017-09-11 09:36:00',21.6 union all
select '2017-09-11 09:36:00',53 union all
select '2017-09-11 09:36:00',47 union all
select '2017-09-11 09:36:00',123 union all
select '2017-09-11 09:36:00',1870 union all
select '2017-09-11 09:39:00',248 union all
select '2017-09-11 09:39:00',23 union all
select '2017-09-11 09:39:00',63 union all
select '2017-09-11 09:39:00',61 union all
select '2017-09-11 09:39:00',44 union all
select '2017-09-11 09:39:00',27
--测试数据结束
SELECT MAX(t.ctime) AS ctime ,
t.grn
FROM ( SELECT ctime ,
COUNT(1) AS grn
FROM #tab
GROUP BY ctime
) t
GROUP BY t.grn;
SELECT MAX(CTIME)
FROM (
SELECT CTIME,
COUNT(1) AS ROWNUMS
FROM #tab
GROUP BY ctime
having COUNT(1) = (
SELECT MAX(ROWNUMS)
FROM (
SELECT CTIME,
COUNT(*) AS ROWNUMS
FROM #tab
GROUP BY
CTIME
) A
)
) a
你要只查询时间的话,下面这样更简单
SELECT TOP 1 ctime FROM #tab
GROUP BY ctime
ORDER BY COUNT(1) desc,ctime DESC
use Tempdb
go
--> --> 听雨停了-->测试数据
if not object_id(N'Tempdb..#tab') is null
drop table #tab
Go
Create table #tab([ctime] Datetime,[value] decimal(18,7))
Insert #tab
select '2017-09-11 09:34:00',23 union all
select '2017-09-11 09:34:00',220 union all
select '2017-09-11 09:34:00',109 union all
select '2017-09-11 09:36:00',28.9 union all
select '2017-09-11 09:36:00',21.6 union all
select '2017-09-11 09:36:00',53 union all
select '2017-09-11 09:36:00',47 union all
select '2017-09-11 09:36:00',123 union all
select '2017-09-11 09:36:00',1870 union all
select '2017-09-11 09:39:00',248 union all
select '2017-09-11 09:39:00',23 union all
select '2017-09-11 09:39:00',63 union all
select '2017-09-11 09:39:00',61 union all
select '2017-09-11 09:39:00',44 union all
select '2017-09-11 09:39:00',27
--测试数据结束
SELECT * FROM #tab WHERE ctime=(
SELECT TOP 1 ctime FROM #tab
GROUP BY ctime
ORDER BY COUNT(1) desc,ctime DESC
)