查看并合并同一个表中的2行1

时间:2015-04-24 09:05:21

标签: c# sql sql-server-2008

我有问题要查看我想要的数据,

IDFlight | Dep1 | Des1| Date  | IDFlight2 | Dep2 | Des2 | Date   | Price
---------+------+-----+-------+-----------+------+------+--------+--------
    2    |  AYT | PRN |20.3.15|     3     | PRN  | AYT  | 27.3.15|  150
    2    |  AYT | PRN |20.3.15|     4     | PRN  | AYT  | 30.3.15|  150
    1    |  AYT | PRN |23.3.15|     4     | PRN  | AYT  | 30.3.15|  150 
    1    |  AYT | PRN |17.3.15|     3     | PRN  | AYT  | 27.3.15|  150

所以搜索查询的日期为+ - 3天,均为2个航班。

在我的情况下,每个航班都在表航班中单独登记,每个航班都有他的航班号和他的方向,日期和价格(例如返程和单程)。 现在这里的问题是,当用户选择返回选项时,将在一行中显示2个航班,(航班1前往目的地,航班2从目的地返回)也在图中示例中的日期行之间存在差异

我想要实现的是显示如上例所示的数据,即每一种方式"记录应与"匹配返回"记录。即使重复第一个数据。

我做了很多研究但没有结果, 我试图用视图但没有成功

我试过联盟没有成功。

@prmDepDay int, @prmDesDay int, @prmDateDep datetime, @prmFrom int, @prmTo int, 
@prmDateRe datetime, @prmFromRe int, @prmToRe int, @prmTotalRe int 
AS
BEGIN
DROP TABLE departureflights

SELECT TOP(100) PERCENT 
             t_flights.idflight, 
             t_flights.flightnumber, 
             t_departureairport.depairportname, 
             t_destinationairport.desairportname, 
             t_flights.startdate, 
             t_flights.totalseats 
  INTO       departureflights 
  FROM       t_flights 
  INNER JOIN t_departureairport 
  ON         t_flights.iddepartureairport = t_departureairport.iddepartureairport 
  INNER JOIN t_destinationairport 
  ON         t_flights.iddestinationairport = t_destinationairport.iddestinationairport 
  INNER JOIN t_flightdirections 
  ON         t_flights.iddirection = t_flightdirections.iddirection 
  WHERE      t_departureairport.iddepartureairport = @prmFrom 
  AND        t_destinationairport.iddestinationairport = @prmTo 
  AND        startdate >= Dateadd(day,-@prmDepDay,@prmDateDep) 
  AND        startdate <= Dateadd(day,@prmDepDay,@prmDateDep) 
             --and TotalSeats>= @prmTotal 
  ORDER BY   t_flights.startdate

  DROP TABLE returnflights        

  SELECT     t_flights.idflight, 
             t_flights.flightnumber              AS ReFlightNumber, 
             t_departureairport.depairportname   AS ReDepAirportName, 
             t_destinationairport.desairportname AS ReDesAirportName, 
             t_flights.enddate , 
             t_flights.totalseats 
  INTO       returnflights 
  FROM       t_flights 
  INNER JOIN t_departureairport 
  ON         t_flights.iddepartureairport = t_departureairport.iddepartureairport 
  INNER JOIN t_destinationairport 
  ON         t_flights.iddestinationairport = t_destinationairport.iddestinationairport 
  INNER JOIN t_flightdirections 
  ON         t_flights.iddirection = t_flightdirections.iddirection 
  WHERE      t_departureairport.iddepartureairport = @prmFromRe 
  AND        t_destinationairport.iddestinationairport = @prmToRe 
  AND        enddate >= Dateadd(day,-@prmDesDay,@prmDateRe) 
  AND        enddate <= Dateadd(day,@prmDesDay,@prmDateRe) 
  AND        totalseats>= @prmTotalRe 
  ORDER BY   t_flights.enddate

1 个答案:

答案 0 :(得分:1)

如果你自己加入t_flights表,你应该在一行中获得传出和返回的航班信息。

SELECT journey_out.idflight IDFlight, 
    journey_out.iddepartureairport Dep1, 
    journey_out.iddestinationairport Des1, 
    journey_out.enddate Date1,
    journey_return.idflight IDFlight2, 
    journey_return.iddepartureairport Dep2, 
    journey_return.iddestinationairport Dep2, 
    journey_return.enddate Date2
FROM t_flights journey_out
INNER JOIN t_flights journey_return 
    ON journey_out.iddestinationairport = journey_return.iddepartureairport 
    AND journey_out.enddate < journey_return.startdate
ORDER BY journey_out.startdate

第一个加入条件确保航班从正确的机场回家,第二个条件确保回程不会在到达之前开始。

如果要在返回选项的同一结果集中查看单向选项,可以将其更改为LEFT JOIN而不是INNER JOIN。