Advertisement
Guest User

Untitled

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