Advertisement
Guest User

Untitled

a guest
Mar 24th, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.91 KB | None | 0 0
  1. USE [Trains]
  2. GO
  3. /****** Object:  StoredProcedure [dbo].[spGetFaresFrom]    Script Date: 25/03/2013 00:20:32 ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER proc
  9. [dbo].[spGetFaresFrom]
  10.     @OriginStation CHAR(3),
  11.     @TicketCode CHAR(3) = NULL,
  12.     @Seasons bit = 0,
  13.     @IncludeAdvances bit = 0,
  14.     @RouteCode INT = NULL,
  15.     @TicketClass INT = NULL,
  16.     @TOCId CHAR(3) = NULL
  17. AS
  18. DECLARE @OriginNlc CHAR(4)
  19. /* Get the code for the specified station */
  20. SELECT
  21.     @OriginNlc = NLCCode
  22. FROM
  23.     Locations
  24. WHERE
  25.     CRSCODE = @OriginStation
  26.  
  27. /* Where a clustered flow exists between two destinations, and a specific one, the clustered flow is completely suppressed */
  28.  
  29. DECLARE @OriginNLCs TABLE
  30. (
  31.     NLC CHAR(4)
  32. )
  33.  
  34.  
  35. DECLARE @DestinationNLCs TABLE
  36. (
  37.     NLC CHAR(4)
  38. )
  39.  
  40. INSERT
  41.     @OriginNLCs
  42. SELECT
  43.     @OriginNlc
  44.  
  45. INSERT
  46.     @OriginNLCs
  47. SELECT
  48.     GroupLocationNLCCode
  49. FROM
  50.     LocationGroupsLocationMembers
  51. WHERE
  52.     MemberLocationNLCCode = @OriginNlc
  53.  
  54. CREATE TABLE #Flows
  55. (
  56. FlowId INT NOT NULL,
  57. OriginNLCCode CHAR(4) NOT NULL,
  58. DestinationNLCCode CHAR(4) NOT NULL,
  59. RouteCode INT,
  60. Cluster bit,
  61. Overridden bit,
  62. TOC CHAR(3) NULL,
  63. CrossLondon tinyint)
  64.  
  65. INSERT INTO
  66.     #Flows
  67. SELECT
  68.     Flows.FlowId,
  69.     OriginFlowCodes.NLC,
  70.     isnull(Flows.DestinationLocation, Dest.LocationNLCCode) DestinationNLCCode,
  71.      Flows.RouteCode,
  72.      1 AS Cluster,
  73.      0 Overriden,
  74.      Flows.TicketTOCId,
  75.      Flows.CrossLondon
  76. FROM
  77.     @OriginNLCs AS OriginFlowCodes
  78.     JOIN ClusterLocations OriginCluster ON OriginCluster.LocationNLCCode = OriginFlowCodes.NLC
  79.     JOIN Flows ON Flows.OriginCluster = OriginCluster.ClusterId
  80.     LEFT JOIN ClusterLocations Dest ON Dest.ClusterId = Flows.DestinationCluster
  81. UNION
  82. SELECT
  83.     Flows.FlowId,
  84.     DestFlowCodes.NLC OriginNLCCode,
  85.     isnull(Flows.OriginLocation, Origin.LocationNLCCode) DestinationNLCCode,
  86.      Flows.RouteCode,
  87.      1 AS Cluster,
  88.      0 Overriden,
  89.      Flows.TicketTOCId,
  90.      Flows.CrossLondon
  91. FROM
  92.     @OriginNLCs AS DestFlowCodes
  93.     JOIN ClusterLocations DestCluster ON DestCluster.LocationNLCCode = DestFlowCodes.NLC
  94.     JOIN Flows ON Flows.DestinationCluster = DestCluster.ClusterId
  95.     LEFT JOIN ClusterLocations Origin ON Origin.ClusterId = Flows.OriginCluster
  96. WHERE
  97.     (@Seasons = 1 OR Flows.Reversable = 1)
  98.  
  99. INSERT INTO #Flows
  100. SELECT
  101.     Flows.FlowId,
  102.     OriginFlowCodes.NLC,
  103.     isnull(Flows.DestinationLocation, Dest.LocationNLCCode) DestinationNLCCode,
  104.      Flows.RouteCode,
  105.      0 AS Cluster,
  106.      0 Overriden,
  107.      Flows.TicketTOCId,
  108.      Flows.CrossLondon
  109. FROM
  110.     @OriginNLCs AS OriginFlowCodes
  111.     JOIN Flows ON Flows.OriginLocation = OriginFlowCodes.NLC
  112.     LEFT JOIN ClusterLocations Dest ON Dest.ClusterId = Flows.DestinationCluster
  113. UNION
  114. SELECT
  115.     Flows.FlowId,
  116.     Flows.DestinationLocation OriginNLCCode,
  117.     isnull(Flows.OriginLocation, Origin.LocationNLCCode) DestinationNLCCode,
  118.      Flows.RouteCode,
  119.      0 AS Cluster,
  120.      0 Overriden,
  121.      Flows.TicketTOCId,
  122.      Flows.CrossLondon
  123. FROM
  124.     @OriginNLCs AS DestFlowCodes
  125.     JOIN Flows ON Flows.DestinationLocation = DestFlowCodes.NLC
  126.     LEFT JOIN ClusterLocations Origin ON Origin.ClusterId = Flows.OriginCluster
  127. WHERE
  128.     (@Seasons = 1 OR Flows.Reversable = 1)
  129.    
  130.  
  131. UPDATE
  132.     F1
  133. SET
  134.     Overridden = 1
  135. FROM
  136.     #Flows F1 JOIN
  137.     #Flows F2 ON F2.OriginNLCCode = F1.OriginNLCCode AND f2.DestinationNLCCode = f1.DestinationNLCCode AND f1.RouteCode = f2.RouteCode
  138. WHERE
  139.     f1.Cluster = 1 AND f2.Cluster = 0
  140.  
  141. CREATE TABLE #Fares
  142. (
  143. OriginNLCCode CHAR(4) NOT NULL,
  144. DestinationNLCCode CHAR(4) NOT NULL,
  145. Fare INT NULL,
  146. RouteCode INT,
  147. TicketCode CHAR(3),
  148. RestrictionCode CHAR(2),
  149. Override bit,
  150. Overridden bit,
  151. TOC CHAR(3) NULL,
  152. CrossLondon tinyint
  153. )
  154.  
  155. INSERT INTO #Fares
  156. SELECT
  157. OriginNLCCode,
  158. DestinationNLCCode,
  159. Fare,
  160. RouteCode,
  161. FlowFares.TicketType,
  162. FlowFares.RestrictionCode,
  163. 0,
  164. Flows.Overridden,
  165. Flows.TOC,
  166. Flows.CrossLondon
  167. FROM #Flows Flows
  168. JOIN FlowFares ON FlowFares.FlowId = Flows.FlowId
  169.  
  170. INSERT INTO
  171. #Fares
  172. SELECT Origin.NLC, NonDerivableFares.DestinationNLCCode, AdultFare, RouteCode, TicketCode, NonDerivableFares.RestrictionCode,
  173. 1, 0, NULL, NonDerivableFares.CrossLondon
  174. FROM
  175.     NonDerivableFares
  176.     JOIN @OriginNLCs Origin ON Origin.NLC = NonDerivableFares.OriginNLCCode
  177. WHERE
  178.     NonDerivableFares.RailcardCode =''
  179.  
  180. UPDATE
  181. F1
  182.     SET
  183.     Overridden = 1
  184. FROM
  185. #Fares f1
  186. JOIN #Fares f2 ON
  187.     f1.OriginNLCCode = f2.OriginNLCCode AND
  188.     f1.DestinationNLCCode = f2.DestinationNLCCode AND
  189.     f1.TicketCode = f2.TicketCode AND
  190.     f1.RouteCode = f2.RouteCode
  191. WHERE
  192.     f2.Override = 1 AND f1.override = 0
  193.  
  194. SELECT
  195.     Origin.Description Origin,
  196.     Fares.OriginNLCCode,  Destination.Description Destination,
  197.     Fares.DestinationNLCCode,
  198.     TicketTypes.Description Ticket, TicketCode,  '£'+CAST(CAST(Fares.fare/100.0 AS DECIMAL(8,2)) AS VARCHAR(10)) Fare,
  199.     Routes.Description Route,
  200.     Routes.Code RouteCode,
  201.     Fares.RestrictionCode,
  202.     TOCs.name TOC,
  203.     TOCs.ID TOCid,
  204.     TicketValidity.BreakOfJourneyOutbound,
  205.     TicketValidity.BreakOfJourneyReturn,
  206.     CASE CrossLondon WHEN 0 THEN 'No' WHEN 1 THEN 'Yes' WHEN 2 THEN 'Not Underground' END AS CrossLondon
  207. FROM
  208.     #Fares Fares
  209.     JOIN Routes ON Routes.Code = Fares.RouteCode
  210.     JOIN TicketTypes ON TicketTypes.code = Fares.TicketCode
  211.     JOIN TicketValidity ON TicketTypes.ValidityCode = TicketValidity.Code
  212.     JOIN Locations Origin ON Origin.NLCCode = fares.OriginNLCCode
  213.     JOIN Locations Destination ON Destination.NLCCode = Fares.DestinationNLCCode
  214.     LEFT JOIN @DestinationNLCs DestNLCs ON DestNLCs.NLC = Fares.DestinationNLCCode
  215.     LEFT JOIN ticketTOCs tocs ON TOCS.Id = Fares.TOC
  216. WHERE
  217.     Fares.fare % 10 = 0 AND fare != 999900 AND Overridden = 0 AND
  218.     ((@Seasons = 0 AND TicketTypes.TicketType != 'N') OR (@Seasons = 1 AND TicketTypes.TicketType = 'N')) AND
  219.     (@IncludeAdvances = 1 OR TicketTypes.RestrictedByTrain = 0) AND
  220.     (@TicketCode IS NULL OR TicketTypes.Code = @TicketCode) AND
  221.     (@TicketClass IS NULL OR TicketTypes.TicketClass = @TicketClass) AND
  222.     (@RouteCode IS NULL OR Routes.Code = @RouteCode) AND
  223.     (@Tocid IS NULL OR Fares.TOC = @Tocid)
  224. ORDER BY Fares.Fare
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement