仅按时间查找sql​​ server中的时差

时间:2016-06-24 06:33:38

标签: sql sql-server

我有一个火车数据库,其中我有许多列车详细信息,包括Train_NoStation_NameArrival_TimeDepature_TimeSource_Station和{{ 1}}。我需要的是我能够使用Destination_Station找到的时差,但如果火车从DATEDIFF(MI,Starttime,EndTime)的特定火车站开始并到达{{1}的出发站,我需要这样的情况},所以如果我发现时间差异是13:15 PM,但在我的情况下,它应该显示13:45 PM,因为火车在其他日期到达了目的地,但我只有时间没有找到的日期这个。为清楚了解,我附上了样本数据 "数据:

30 Minutes

因此,如果您看到上述数据,我们可以选择一个人从列车号1470 Minutes的{​​{1}}开始,并希望在Train No.,Train Name,islno,station Code,Station Name,Arrival time,Departure time,Distance,Source Station Code,Source Station Name,Destination station Code,Destination Station Name,Distance '11096',AHIMSA EXPRESS ,1,PUNE,PUNE JN ,'00:00:00','19:50:00',0,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,64 '11096',AHIMSA EXPRESS ,2,LNL ,LONAVALA ,'20:43:00','20:45:00',64,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,28 '11096',AHIMSA EXPRESS ,3,KJT ,KARJAT ,'21:28:00','21:30:00',92,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,47 '11096',AHIMSA EXPRESS ,4,KYN ,KALYAN JN ,'22:17:00','22:20:00',139,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,25 '11096',AHIMSA EXPRESS ,5,BIRD,BHIWANDI ROAD ,'22:48:00','22:50:00',164,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,27 '11096',AHIMSA EXPRESS ,6,BSR ,VASAI ROAD ,'23:45:00','23:50:00',191,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,72 '11096',AHIMSA EXPRESS ,7,DRD ,DAHANU ROAD ,'01:08:00','01:10:00',263,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,51 '11096',AHIMSA EXPRESS ,8,VAPI,VAPI ,'01:43:00','01:45:00',314,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,24 '11096',AHIMSA EXPRESS ,9,BL ,VALSAD ,'02:11:00','02:13:00',338,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,39 '11096',AHIMSA EXPRESS ,10,NVS ,NAVSARI ,'03:01:00','03:03:00',377,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,29 '11096',AHIMSA EXPRESS ,11,ST ,SURAT ,'03:50:00','03:55:00',406,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,59 '11096',AHIMSA EXPRESS ,12,BH ,BHARUCH JN ,'04:39:00','04:41:00',465,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,70 '11096',AHIMSA EXPRESS ,13,BRC ,VADODARA JN ,'05:40:00','05:45:00',535,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,36 '11096',AHIMSA EXPRESS ,14,ANND,ANAND JN ,'06:19:00','06:21:00',571,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,18 '11096',AHIMSA EXPRESS ,15,ND ,NADIAD JN ,'06:39:00','06:41:00',589,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,43 '11096',AHIMSA EXPRESS ,16,MAN ,MANINAGAR ,'07:23:00','07:25:00',632,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,3 '11096',AHIMSA EXPRESS ,17,ADI ,AHMEDABAD JN ,'07:45:00','00:00:00',635,PUNE,PUNE JN ,ADI ,AHMEDABAD JN ,-635 '11098',POORNA EXPRESS ,1,ERS ,ERNAKULAM JN ,'00:00:00','23:30:00',0,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,20 '11098',POORNA EXPRESS ,2,AWY ,ALUVA ,'23:51:00','23:53:00',20,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,55 '11098',POORNA EXPRESS ,3,TCR ,THRISUR ,'00:38:00','00:41:00',75,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,32 '11098',POORNA EXPRESS ,4,SRR ,SHORANUR JN ,'01:30:00','01:35:00',107,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,45 '11098',POORNA EXPRESS ,5,TIR ,TIRUR ,'02:14:00','02:16:00',152,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,41 '11098',POORNA EXPRESS ,6,CLT ,KOZHIKODE ,'03:05:00','03:10:00',193,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,69 '11098',POORNA EXPRESS ,7,TLY ,THALASSERY ,'04:02:00','04:03:00',262,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,21 '11098',POORNA EXPRESS ,8,CAN ,KANNUR ,'04:35:00','04:40:00',283,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,33 '11098',POORNA EXPRESS ,9,PAY ,PAYYANUR ,'05:08:00','05:10:00',316,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,29 '11098',POORNA EXPRESS ,10,KZE ,KANHANGAD ,'05:35:00','05:37:00',345,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,23 '11098',POORNA EXPRESS ,11,KGQ ,KASARAGOD ,'05:53:00','05:55:00',368,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,51 '11098',POORNA EXPRESS ,12,MAJN,MANGALORE JN ,'07:05:00','07:15:00',419,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,36 '11098',POORNA EXPRESS ,13,MULK,MULKI ,'07:44:00','07:45:00',455,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,46 '11098',POORNA EXPRESS ,14,UD ,UDUPI ,'08:40:00','08:42:00',501,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,44 '11098',POORNA EXPRESS ,15,KUDA,KUNDAPURA ,'09:28:00','09:30:00',545,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,69 '11098',POORNA EXPRESS ,16,BTJL,BHATKAL ,'10:20:00','10:22:00',614,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,21 '11098',POORNA EXPRESS ,17,MRDW,MURDESHWAR ,'10:40:00','10:42:00',635,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,37 '11098',POORNA EXPRESS ,18,HNA ,HONNAVAR ,'11:10:00','11:12:00',672,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,19 '11098',POORNA EXPRESS ,19,KT ,KUMTA ,'11:40:00','11:42:00',691,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,27 '11098',POORNA EXPRESS ,20,GOK ,GOKARNA ROAD ,'12:00:00','12:02:00',718,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,50 '11098',POORNA EXPRESS ,21,KAWR,KARWAR ,'12:50:00','12:52:00',768,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,82 '11098',POORNA EXPRESS ,22,MAO ,MADGAON ,'14:05:00','14:25:00',850,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,16 '11098',POORNA EXPRESS ,23,SVM ,SANVERDAM CHUCH,'14:38:00','14:40:00',866,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,18 '11098',POORNA EXPRESS ,24,QLM ,KULEM ,'15:05:00','15:10:00',884,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,59 '11098',POORNA EXPRESS ,25,CLR ,CASTLE ROCK ,'16:05:00','16:10:00',943,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,24 '11098',POORNA EXPRESS ,26,LD ,LONDA JN ,'16:48:00','16:50:00',967,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,51 '11098',POORNA EXPRESS ,27,BGM ,BELGAUM ,'18:55:00','19:00:00',1018,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,58 '11098',POORNA EXPRESS ,28,GPB ,GHATPRABHA ,'19:58:00','20:00:00',1076,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,80 '11098',POORNA EXPRESS ,29,MRJ ,MIRAJ JN ,'22:55:00','23:00:00',1156,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,7 '11098',POORNA EXPRESS ,30,SLI ,SANGLI ,'23:12:00','23:15:00',1163,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,68 '11098',POORNA EXPRESS ,31,KRD ,KARAD ,'00:12:00','00:15:00',1231,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,59 '11098',POORNA EXPRESS ,32,STR ,SATARA ,'01:07:00','01:10:00',1290,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,145 '11098',POORNA EXPRESS ,33,PUNE,PUNE JN ,'05:05:00','00:00:00',1435,ERS ,ERNAKULAM JN ,PUNE,PUNE JN ,-1435 '12548',AGRA SUPER FAST,1,ADI ,AHMEDABAD JN ,'00:00:00','16:55:00',0,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,68 '12548',AGRA SUPER FAST,2,MSH ,MAHESANA JN ,'17:58:00','18:00:00',68,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,65 '12548',AGRA SUPER FAST,3,PNU ,PALANPUR JN ,'19:36:00','19:38:00',133,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,53 '12548',AGRA SUPER FAST,4,ABR ,ABU ROAD ,'21:00:00','21:12:00',186,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,99 '12548',AGRA SUPER FAST,5,FA ,FALNA ,'22:29:00','22:31:00',285,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,66 '12548',AGRA SUPER FAST,6,MJ ,MARWAR JN ,'23:30:00','23:32:00',351,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,88 '12548',AGRA SUPER FAST,7,BER ,BEAWAR ,'00:57:00','00:59:00',439,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,52 '12548',AGRA SUPER FAST,8,AII ,AJMER JN ,'02:00:00','02:10:00',491,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,135 '12548',AGRA SUPER FAST,9,JP ,JAIPUR ,'04:05:00','04:15:00',626,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,187 '12548',AGRA SUPER FAST,10,BTE ,BHARATPUR JN ,'06:48:00','06:50:00',813,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,54 '12548',AGRA SUPER FAST,11,AF ,AGRA FORT ,'08:20:00','00:00:00',867,ADI ,AHMEDABAD JN ,AF ,AGRA FORT ,-867" 离开时我需要找到时间差我怎么能这样做?

2 个答案:

答案 0 :(得分:4)

例如,您可以创建一个视图,其中包含如下所示的列:

select 
  sum(Days) over (partition by [Train No] order by Distance) as Days, *
from (
  select
    case when lag([Arrival time]) over (partition by [Train No] order by Distance) > [Arrival time] 
      then 1 else 0 end as Days, *
  from
    yourtable
) X

这会检查Arrival time已减少的行,因此必须是新的一天,然后将1加到列中。外部选择然后执行日期的运行总计,因此它将累计所有日期。然后在日期计算中使用该列。

没有测试过,但它应该可以正常工作。

答案 1 :(得分:0)

如果您使用SQL Server 2012+,则可以查看LEADLAG。如果你需要一个跟随或前一行,那就太帅了。

问题:

如果有超过24小时的连接可能是正确的,那么您将需要存储某种大约的持续时间" (或者至少 isMoreThan24Hours 指示)在边桌中(对所有工作站使用CROSS JOIN并平均现有数据。其余的是手工工作。

如果您确定,没有任何关系可以持久,您应该对JamesZ的答案感到满意......