/****** Script for SelectTopNRows command from SSMS ******/
SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
select *
from [QTKCenter].[dbo].[SpendHistory] a
where exists (select 1 from (
select max(sendtime)sendtime,extime,exstantion
from [QTKCenter].[dbo].[SpendHistory]
group by extime,exstantion)b where a.extime=b.extime and a.exstantion=b.exstantion
and a.sendtime=b.sendtime) and [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
------解决方案--------------------
select a.*
from
(SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
)a
inner join
(
SELECT Extime,ExStantion,max(sendTime) as sendTime
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
group by Extime,ExStantion
)b on a.Extime=b.Extime and a.ExStantion=b.ExStantion and a.sendTime=b.sendTime
------解决方案-------------------- with cte as(
select *,row_number() over(partition by Extime,ExStantion
order by sendTime desc) as [rank]
from [QTKCenter].[dbo].[SpendHistory]
)
select * from cte where rank=1 ------解决方案--------------------
select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
,[DealStatus],[SendTime] from
(SELECT [CardNo]
,[CardID]
,[CardSave]
,[TollAmount]
,[ExTime]
,[ExRoad]
,[ExStation]
,[ExShiftDate]
,[EnTime]
,[EnRoad]
,[EnStation]
,[ExVehPlate]
,[VerifyCode]
,[DealStatus]
,[SendTime]
,row_number() over(partition by Extime,ExStantion order by sendTime desc) 'rn'
FROM [QTKCenter].[dbo].[SpendHistory]
where [CardNo] ='6212262409000028515' and [ExTime] between '2013-3-1' and '2013-3-31'
) t where rn=1
------解决方案--------------------
你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。
另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31'
后大概还剩下多少呢 ------解决方案--------------------