-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery.sql
126 lines (124 loc) · 4.12 KB
/
query.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
DECLARE @carrier_code VARCHAR(2) = @p1;
DECLARE @currency CHAR(3) = @p2;
SELECT
i.CarrierCode AS carrier,
i.DepartureStation AS origin,
i.ArrivalStation AS destination,
i.FlightNumber AS flight_number,
i.STD AS date_departure,
i.SeatsSold AS seats_sold,
i.SeatsAvailable AS seats_available,
r.Revenue AS revenue,
ar.Revenue AS ancillary_revenue
FROM (
SELECT
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.FlightNumber,
il.STD,
SUM(ic.ClassSold) AS SeatsSold,
il.AdjustedCapacity - SUM(ic.ClassSold) AS SeatsAvailable
FROM InventoryLeg AS il
INNER JOIN InventoryLegClass AS ic WITH ( NOLOCK )
ON il.InventoryLegID = ic.InventoryLegID
WHERE
il.CarrierCode = @carrier_code
AND il.Status <= 1
GROUP BY
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.STD,
il.FlightNumber,
il.AdjustedCapacity
) AS i
INNER JOIN
(
SELECT
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.FlightNumber,
il.STD,
SUM(pjc.ChargeAmount * cc.ConversionRate) AS Revenue
FROM Booking AS b
INNER JOIN BookingPassenger AS bp WITH ( NOLOCK )
ON b.BookingID = bp.BookingID
INNER JOIN PassengerJourneySegment AS pjs WITH ( NOLOCK )
ON bp.PassengerID = pjs.PassengerID
INNER JOIN PassengerJourneyCharge AS pjc WITH ( NOLOCK )
ON pjs.PassengerID = pjc.PassengerID
AND pjs.SegmentID = pjc.SegmentID
INNER JOIN Currency AS c
ON c.CurrencyCode = pjc.CurrencyCode
INNER JOIN CurrencyConversion AS cc
ON cc.FromCurrencyCode = c.CurrencyCode
AND cc.ToCurrencyCode = @currency
INNER JOIN PassengerJourneyLeg AS pjl WITH ( NOLOCK )
ON pjs.PassengerID = pjl.PassengerID
AND pjs.SegmentID = pjl.SegmentID
INNER JOIN InventoryLeg AS il WITH ( NOLOCK )
ON pjl.InventoryLegID = il.InventoryLegID
AND pjl.LegNumber = il.LegNumber
WHERE
il.CarrierCode = @carrier_code
AND il.Status <= 1
GROUP BY
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.FlightNumber,
il.STD
) AS r
ON i.CarrierCode = r.CarrierCode
AND i.DepartureStation = r.DepartureStation
AND i.ArrivalStation = r.ArrivalStation
AND i.FlightNumber = r.FlightNumber
AND i.STD = r.STD
INNER JOIN
(
SELECT
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.FlightNumber,
il.STD,
SUM(pfc.ChargeAmount * cc.ConversionRate) AS Revenue
FROM Booking AS b
INNER JOIN BookingPassenger AS bp WITH ( NOLOCK )
ON b.BookingID = bp.BookingID
INNER JOIN PassengerFee AS pf WITH ( NOLOCK )
ON bp.PassengerID = pf.PassengerID
INNER JOIN PassengerFeeCharge AS pfc WITH ( NOLOCK )
ON pf.PassengerID = pfc.PassengerID
AND pf.FeeNumber = pfc.FeeNumber
INNER JOIN Currency AS c
ON c.CurrencyCode = pfc.CurrencyCode
INNER JOIN CurrencyConversion AS cc
ON cc.FromCurrencyCode = c.CurrencyCode
AND cc.ToCurrencyCode = @currency
INNER JOIN PassengerJourneySegment AS pjs WITH ( NOLOCK )
ON bp.PassengerID = pjs.PassengerID
INNER JOIN PassengerJourneyLeg AS pjl WITH ( NOLOCK )
ON pjs.PassengerID = pjl.PassengerID
AND pjs.SegmentID = pjl.SegmentID
INNER JOIN InventoryLeg AS il WITH ( NOLOCK )
ON pf.InventoryLegID = il.InventoryLegID
AND pjl.LegNumber = il.LegNumber
WHERE
il.CarrierCode = @carrier_code
AND il.Status <= 1
GROUP BY
il.CarrierCode,
il.DepartureStation,
il.ArrivalStation,
il.FlightNumber,
il.STD
) AS ar
ON i.CarrierCode = ar.CarrierCode
AND i.DepartureStation = ar.DepartureStation
AND i.ArrivalStation = ar.ArrivalStation
AND i.FlightNumber = ar.FlightNumber
AND i.STD = ar.STD
WHERE i.STD >= DATEADD(DAY, -3, current_timestamp);