|
-Build a table (only create related fields)
create table T_Schedule_Detail
(
cid int,
ScheduleCode varchar(4),
cityName varchar(10)
)
insert into T_Schedule_Detail select 1,'T108','Beijing'
insert into T_Schedule_Detail select 2,'T108','Zhengzhou'
insert into T_Schedule_Detail select 3,'T108','Changsha'
insert into T_Schedule_Detail select 4,'T108','Hainan'
insert into T_Schedule_Detail select 5,'T989','Changsha'
insert into T_Schedule_Detail select 6,'T989','Chongqing'
--Inquire
select a.ScheduleCode ,a.cityname ,b.ScheduleCode, b.cityname
from T_Schedule_Detail a left join T_Schedule_Detail b
on a.cityName = b.cityName and a.ScheduleCode <> b.ScheduleCode
where exists (select 1 from T_Schedule_Detail
where ScheduleCode = a.ScheduleCode and cityName ='Beijing') and
exists (select 1 from T_Schedule_Detail
where ScheduleCode = b.ScheduleCode and cityName ='Chongqing')
--result
ScheduleCode cityname ScheduleCode cityname
------------ ---------- ------------ ----------
T108 Changsha T989 Changsha
(The number of rows affected is 1 row)
--It means: take T108 times and transfer to T989 times in Changsha |
|