Train_status_new表包含火车经过的车站代码的(4000+)详细信息。
Train_status_new
RunId TrainNumber OriginStation_Name PrevStation_Name CurrentStation_Name NextStation_Name DestinationStation_Name
1 6063 MS VM NPM TDPR NCJ
1 6853 KIK TMPT NCR NULL NCR
2 6027 TBM VM ICG TPJ QLN
2 12605 MS GOC TPJ PDKT KKDI
3 12631 MS TEN ICG TPJ TEN
3 12654 TPJ KKPM ALU PNDM MS
Station_List具有(700+)个代码和站点名称。
Station_List
StationCode StationName
TDPR TIRUPADRIPULYUR
KKDI Karaikkudi
PDKT PUDUKKOTTAI
ALU Ariyalur
MS Chennai Egmore
NCJ Nagerkoil Junction
PNDM Pennadam
QLN Kollam Junction
TBM Tambaram
TEN Tirunelveli Junction
TPJ Tiruchirappalli Junction
VM Villupuram Junction
KKPM Kallakudi Plgh
GOC Golden Rock
现在,我想要Train_status_new
中的站名代码名称。所以,我有这个查询,但是我有重复。
;with stationList as
(
select StationId, StationCode code, StationName from Station_List st
)
, origin as
(
select * from StationList st
join train_Status_New tr
on st.Code = tr.OriginStation_Name
)
, pre as
(
select st.StationName, tr.RunId, tr.TrainNumber, tr.PrevStation_Name
, tr.PrevStation_Act_Arr_Time, tr.PrevStation_Act_Dep_Time, tr.PrevStation_Sch_Arr_Time, tr.PrevStation_Sch_Dep_Time
from StationList st
join train_Status_New tr
on st.Code = tr.PrevStation_Name
)
, cur as
(
select st.StationName, tr.RunId, tr.TrainNumber, tr.CurrentStation_Name
, tr.CurrentStation_Act_Arr_Time, tr.CurrentStation_Act_Dep_Time, tr.CurrentStation_Sch_Arr_Time, tr.CurrentStation_Sch_Dep_Time
from StationList st
join train_Status_New tr
on st.Code = tr.CurrentStation_Name
)
, next as
(
select st.StationName, tr.RunId, tr.TrainNumber, tr.NextStation_Name
, tr.NextStation_Act_Arr_Time, tr.NextStation_Act_Dep_Time, tr.NextStation_Sch_Arr_Time, tr.NextStation_Sch_Dep_Time
from StationList st
join train_Status_New tr
on st.Code = tr.NextStation_Name
)
, dest as
(
select st.StationName, tr.RunId, tr.TrainNumber, tr.DestinationStation_Name
, tr.DestinationStation_Act_Arr_Time, tr.DestinationStation_Act_Dep_Time, tr.DestinationStation_Sch_Arr_Time, tr.DestinationStation_Sch_Dep_Time
from StationList st
join train_Status_New tr
on st.Code = tr.DestinationStation_Name
)
select distinct o.RunId, o.StationCode, o.RunDate, o.TrainNumber, t.Trainname, o.QueryRunDateTime
, o.OriginStation_Name , o.StationName O_StationName
例如,火车6063
具有五个车站代码。因此,我的预期输出包含从Station_List
获取的站点名称。
RunId TrainNumber OriginStation_Name Origin_Name PrevStation_Name Prev_Name CurrentStation_Name Current_Name NextStation_Name Next_Name DestinationStation_Name Destination_Name
1 6063 MS Chennai Egmore VM Villupuram Junction NPM NagaMalai TDPR TIRUPADRIPULYUR NCJ Nagerkoil Junction
仅供参考:
version: Microsoft SQL Server 2012
Train_Status_new
包含25列以上作为时间列。我不能补充。因为这些未在此要求中使用。