以下SQL报告显示为火车站(GoAbbr)提供服务的公交路线(主线表),名为ARGO,以及路线为其服务的时间(st.ETime列)。由于并非所有的公交线路都没有在ARGO开始,我包括了公交线路的开始时间(t.FromTime栏目)。
问题
巴士路线31从ARGO开始,服务多个巴士站并在ARGO结束。公共汽车路线222在ARGO开始并在不同的位置结束。当我运行SQL报告时,它会产生如下结果:
Line FromTime ETime
---- -------- -----
031 16:41 16:41
031 16:41 17:11
222 17:41 17:41
正如你所看到的,SQL报告显示了31号航线从ARGO 16:41出发并于17:11到达ARGO的两个结果,这在技术上是假设的。但是,我想要消除显示重复的FromTime和ETime的Route 31结果。现在,我可以将它添加到我的SQL WHERE语句中:
t.FromTime <> st.ETime
但这会消除Route 222的结果,我不想发生这种结果。我试过寻找答案,但我不知所措。有人可以帮忙吗?
SQL语句
SELECT DISTINCT
sup.signupname, msg.servicegroupabbr, ml.lineabbr, l.DirectionName,
s.stopabbr, s.stopname, t.FromTime, st.ETime, 'ARGO' as GoAbbr
FROM
trips t, linestop ls, stoptimes st, masterservicegroup msg, signupperiods
sup, stops s, masterline ml, line l
WHERE
t.signid = sup.signid and
t.signid = ls.signid and
t.linedirid = ls.linedirid and
t.linedirid = l.linedirid and
l.lineid = ml.lineid and
t.tripid = st.tripid and
ls.stopnum = st.stopnum and
t.servicegroupid = msg.servicegroupid and
ls.stopid = s.stopid and
s.stopabbr IN ('6424','6423','5253','1745','1759') and
ml.lineabbr IN ('031','032','033','033A','054','222') and
order by
s.stopabbr, ml.lineabbr,st.ETime
答案 0 :(得分:0)
你可以这样做的一种方法是使用row_number
函数,然后按linedirid, FromTime, ETime
进行分区,类似这样,也使用正确的join
试一试,如果需要修复连接,让我们知道它是如何工作的
SELECT rownums, signupname, servicegroupabbr, lineabbr,
DirectionName, stopabbr, stopname, FromTime,
ETime, GoAbbr
FROM (
SELECT DISTINCT ROW_NUMBER() OVER
(PARTITION BY t.linedirid, t.FromTime, st.ETime
ORDER BY t.linedirid) rownums,
sup.signupname,
msg.servicegroupabbr,
ml.lineabbr,
l.DirectionName,
s.stopabbr,
s.stopname,
t.FromTime,
st.ETime,
'ARGO' as GoAbbr
FROM trips t
JOIN linestop ls ON t.signid = ls.signid and
t.linedirid = ls.linedirid and
JOIN stops s ON ls.stopid = s.stopid and
s.stopabbr IN ('6424','6423',
'5253','1745','1759')
JOIN line l ON t.linedirid = l.linedirid
JOIN stoptimes st ON t.tripid = st.tripid and
ls.stopnum = st.stopnum
JOIN masterservicegroup msg ON msg.servicegroupid = t.servicegroupid,
JOIN signupperiods sup ON t.signid = sup.signid
JOIN masterline ml ON l.lineid = ml.lineid annd
ml.lineabbr IN ('031','032','033',
'033A','054','222')) src
WHERE rownums = 1
ORDER BY stopabbr, lineabbr, ETime