查询时过滤部分不符合要求的数据

十八道胡同 2013-11-07 01:50:33
我现在可以查询出某时间段内某张的卡所有流水,我的sql如下:
/****** 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'


但是流水里面有些数据我想剔除:对于相同Extime 和ExStantion的流水,只取sendTime最大的那条数据。
想了半个小时了,暂时没想法,各位有什么好办法吗?
...全文
309 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
發糞塗牆 2013-11-08
  • 打赏
  • 举报
回复
2008的话没这个说法,2008之前的话,是你说的这样
十八道胡同 2013-11-08
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
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'
引用 18 楼 LCL_data 的回复:
[quote=引用 16 楼 yupeigu 的回复:] [quote=引用 15 楼 LCL_data 的回复:] [quote=引用 13 楼 yupeigu 的回复:] [quote=引用 12 楼 LCL_data 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s[/quote] 哦 ,呵呵,那返回了多少条数据呢[/quote] 有效数据39条。如果不过滤那些相同的有59条。[/quote] 那建个索引呢,这样:
create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:

select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
       ,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
       ,[DealStatus],[SendTime] 
from
(
	select *,
		   row_number() over(partition by Extime,ExStantion 
								 order by sendTime desc) rownum
	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'
	) t 
)t
where rownum=1
[/quote] 嗯,明天我试试[/quote]
引用 10 楼 ap0405140 的回复:
[quote=引用 6 楼 LCL_data 的回复:] 你的内部select时生成了rn的排名。 如果我的内部循环把所有的数据找到,外层循环在基于内层循环的结果进行rn排序,是不是会快一些?这样rn的计算基数就比较小。
方法2,

SELECT [CardNo]
      ,[CardID]
      ,[CardSave]
      ,[TollAmount]
      ,[ExTime]
      ,[ExRoad]
      ,[ExStation]
      ,[ExShiftDate]
      ,[EnTime]
      ,[EnRoad]
      ,[EnStation]
      ,[ExVehPlate]
      ,[VerifyCode]
      ,[DealStatus]
      ,[SendTime]
  FROM [QTKCenter].[dbo].[SpendHistory] a
  where a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31'
  and not exists
  (select 1 from [QTKCenter].[dbo].[SpendHistory] b
   where b.[CardNo]='6212262409000028515' and b.[ExTime] between '2013-3-1' and '2013-3-31'
   and b.[Extime]=a.[Extime] and b.[ExStantion]=a.[ExStantion] and b.[SendTime]>a.[SendTime])
[/quote] 如果where后面有多个条件,是不是说有索引或者是主键的放前面,速度会快一点?
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 17 楼 DBA_Huangzj 的回复:
执行计划贴出来
这个功能是功能集的一部分,QA测试时发现数据不对,调试发现了这个问题。没有具体的计划。 我会试一下各种方案后在结贴。
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 16 楼 yupeigu 的回复:
[quote=引用 15 楼 LCL_data 的回复:] [quote=引用 13 楼 yupeigu 的回复:] [quote=引用 12 楼 LCL_data 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s[/quote] 哦 ,呵呵,那返回了多少条数据呢[/quote] 有效数据39条。如果不过滤那些相同的有59条。[/quote] 那建个索引呢,这样:
create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:

select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
       ,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
       ,[DealStatus],[SendTime] 
from
(
	select *,
		   row_number() over(partition by Extime,ExStantion 
								 order by sendTime desc) rownum
	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'
	) t 
)t
where rownum=1
[/quote] 嗯,明天我试试
發糞塗牆 2013-11-07
  • 打赏
  • 举报
回复
执行计划贴出来
  • 打赏
  • 举报
回复
引用 15 楼 LCL_data 的回复:
[quote=引用 13 楼 yupeigu 的回复:] [quote=引用 12 楼 LCL_data 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s[/quote] 哦 ,呵呵,那返回了多少条数据呢[/quote] 有效数据39条。如果不过滤那些相同的有59条。[/quote] 那建个索引呢,这样:
create index idx_SpendHistory_cc on SpendHistory(CardNo,ExTime)
然后,在运行下面的语句,看看:

select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
       ,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
       ,[DealStatus],[SendTime] 
from
(
	select *,
		   row_number() over(partition by Extime,ExStantion 
								 order by sendTime desc) rownum
	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'
	) t 
)t
where rownum=1
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 13 楼 yupeigu 的回复:
[quote=引用 12 楼 LCL_data 的回复:] [quote=引用 11 楼 yupeigu 的回复:] [quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s[/quote] 哦 ,呵呵,那返回了多少条数据呢[/quote] 有效数据39条。如果不过滤那些相同的有59条。
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 1 楼 DBA_Huangzj 的回复:
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'
你的思路我第一遍 没看懂。现在差不多le
  • 打赏
  • 举报
回复
引用 12 楼 LCL_data 的回复:
[quote=引用 11 楼 yupeigu 的回复:] [quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s[/quote] 哦 ,呵呵,那返回了多少条数据呢
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 11 楼 yupeigu 的回复:
[quote=引用 9 楼 LCL_data 的回复:] [quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢[/quote] 不是我数据表大,我在同事的固态硬盘上跑 用2s,我的本本2分41s
  • 打赏
  • 举报
回复
引用 9 楼 LCL_data 的回复:
[quote=引用 7 楼 DBA_Huangzj 的回复:] 我的不行?
您的我还没测。 我的电脑快卡死了。。。[/quote] 你的表[QTKCenter].[dbo].[SpendHistory]的数据量有多大呢。 另外,加上了过滤条件: a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31' 后大概还剩下多少呢
唐诗三百首 2013-11-07
  • 打赏
  • 举报
回复
引用 6 楼 LCL_data 的回复:
你的内部select时生成了rn的排名。 如果我的内部循环把所有的数据找到,外层循环在基于内层循环的结果进行rn排序,是不是会快一些?这样rn的计算基数就比较小。
方法2,

SELECT [CardNo]
      ,[CardID]
      ,[CardSave]
      ,[TollAmount]
      ,[ExTime]
      ,[ExRoad]
      ,[ExStation]
      ,[ExShiftDate]
      ,[EnTime]
      ,[EnRoad]
      ,[EnStation]
      ,[ExVehPlate]
      ,[VerifyCode]
      ,[DealStatus]
      ,[SendTime]
  FROM [QTKCenter].[dbo].[SpendHistory] a
  where a.[CardNo]='6212262409000028515' and a.[ExTime] between '2013-3-1' and '2013-3-31'
  and not exists
  (select 1 from [QTKCenter].[dbo].[SpendHistory] b
   where b.[CardNo]='6212262409000028515' and b.[ExTime] between '2013-3-1' and '2013-3-31'
   and b.[Extime]=a.[Extime] and b.[ExStantion]=a.[ExStantion] and b.[SendTime]>a.[SendTime])
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 7 楼 DBA_Huangzj 的回复:
我的不行?
您的我还没测。 我的电脑快卡死了。。。
  • 打赏
  • 举报
回复
是这样不:


select [CardNo],[CardID],[CardSave],[TollAmount],[ExTime],[ExRoad],[ExStation]
       ,[ExShiftDate],[EnTime],[EnRoad],[EnStation],[ExVehPlate],[VerifyCode]
       ,[DealStatus],[SendTime] 
from
(
	select *,
		   row_number() over(partition by Extime,ExStantion 
								 order by sendTime desc) rownum
	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'
	) t 
)t
where rownum=1

發糞塗牆 2013-11-07
  • 打赏
  • 举报
回复
我的不行?
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 5 楼 ap0405140 的回复:

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
你的内部select时生成了rn的排名。 如果我的内部循环把所有的数据找到,外层循环在基于内层循环的结果进行rn排序,是不是会快一些?这样rn的计算基数就比较小。
唐诗三百首 2013-11-07
  • 打赏
  • 举报
回复

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
十八道胡同 2013-11-07
  • 打赏
  • 举报
回复
引用 3 楼 rockyljt 的回复:
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
还需要点时间来理解下你的思路。
---涛声依旧--- 2013-11-07
  • 打赏
  • 举报
回复
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
Andy__Huang 2013-11-07
  • 打赏
  • 举报
回复
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 
加载更多回复(1)

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧