Advertisement
IgorPopov1234

Query With Problems

Jun 1st, 2012
491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.77 KB | None | 0 0
  1. SET NOCOUNT ON
  2.  
  3. DECLARE @port1 NVARCHAR(100) = 'port1'
  4. DECLARE @port2 NVARCHAR(100) = 'port2'
  5.  
  6. DECLARE @ship_id INT = 1
  7.  
  8. CREATE TABLE #temp_voyage
  9. (
  10.     voyage_id int,
  11.     departure_port nvarchar(100),
  12.     arrival_port nvarchar(100),
  13.     begin_time datetime,
  14.     end_time datetime
  15. )
  16.  
  17.  
  18. CREATE TABLE #temp_speed_profile
  19. (
  20.     temp_id int identity,
  21.     voyage_id int,
  22.     departure_port nvarchar(100),
  23.     arrival_port nvarchar(100),
  24.     entry_time datetime,
  25.     begin_time datetime,
  26.     end_time datetime,
  27.     speed_prev decimal(18, 3),
  28.     speed_new decimal(18, 3),
  29.     distance decimal(18, 3)
  30. )
  31.  
  32. INSERT INTO #temp_voyage
  33.     (
  34.         voyage_id,
  35.         begin_time,
  36.         end_time
  37.     )
  38.  
  39.     SELECT
  40.         id,
  41.         begin_time,
  42.         end_time
  43.  
  44.     FROM
  45.         ReportVoyage voyage
  46.  
  47.     WHERE
  48.         ship_id = @ship_id AND
  49.         end_time IS NOT NULL
  50.        
  51.     ORDER BY
  52.         id DESC
  53.  
  54.  
  55. DECLARE voyage_id_cursor CURSOR FOR
  56.     SELECT
  57.          voyage_id
  58.    
  59.     FROM
  60.         #temp_voyage
  61.  
  62.  
  63. DECLARE @voyage_id INT
  64. OPEN voyage_id_cursor
  65. FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  66.  
  67. WHILE @@FETCH_STATUS=0
  68. BEGIN
  69.     UPDATE
  70.         #temp_voyage
  71.    
  72.     SET
  73.         departure_port = (
  74.             SELECT TOP 1
  75.                 departure_port
  76.            
  77.             FROM
  78.                 ReportLeg leg
  79.            
  80.             JOIN
  81.                 ReportValue value
  82.             ON
  83.                 value.leg_id = leg.id
  84.            
  85.             WHERE
  86.                 voyage_id = @voyage_id
  87.            
  88.             ORDER BY
  89.                 leg_id ASC
  90.         ),
  91.        
  92.         arrival_port = (
  93.             SELECT TOP 1
  94.                 arrival_port
  95.            
  96.             FROM
  97.                 ReportLeg leg
  98.            
  99.             JOIN
  100.                 ReportValue value
  101.             ON
  102.                 value.leg_id = leg.id
  103.            
  104.             WHERE
  105.                 voyage_id = @voyage_id
  106.            
  107.             ORDER BY
  108.                 leg_id DESC
  109.         )
  110.    
  111.     WHERE
  112.         voyage_id = @voyage_id
  113.    
  114.     FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  115. END
  116.  
  117. CLOSE voyage_id_cursor
  118. DEALLOCATE voyage_id_cursor
  119.  
  120.  
  121. DELETE FROM
  122.     #temp_voyage
  123.  
  124. WHERE
  125.     ((arrival_port   NOT IN (@port1, @port2)) OR
  126.      (departure_port NOT IN (@port1, @port2)))
  127.    
  128.     OR
  129.    
  130.     (@port1 != @port2 AND
  131.         ((arrival_port = @port1 AND
  132.          departure_port = @port1)
  133.          
  134.          OR
  135.          
  136.          (arrival_port = @port2 AND
  137.          departure_port = @port2))
  138.     )
  139.  
  140.  
  141. SELECT
  142.     *
  143. FROM
  144.     #temp_voyage
  145.  
  146.  
  147. -------------------^^^^^ filter out voyages that aren't between ports @port1 and @port2 ^^^^^^---------------------
  148. DECLARE @temp_id INT
  149.  
  150. DECLARE @distance DECIMAL(18, 3)
  151. DECLARE @speed DECIMAL(18, 3)
  152.  
  153.  
  154.  
  155. DECLARE voyage_id_cursor CURSOR FOR
  156.     SELECT TOP 3
  157.          voyage_id
  158.    
  159.     FROM
  160.         #temp_voyage
  161.  
  162. OPEN voyage_id_cursor
  163. FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  164.  
  165. WHILE @@FETCH_STATUS=0
  166. BEGIN
  167.     INSERT INTO #temp_speed_profile
  168.             (voyage_id,
  169.              entry_time,
  170.              speed_prev
  171.             )
  172.            
  173.         SELECT
  174.              voyage_id,
  175.              entry_time,
  176.              value.value
  177.        
  178.         FROM
  179.             ReportValue value
  180.        
  181.         JOIN
  182.             ReportValueType type
  183.         ON
  184.             value.type_id = type.id
  185.        
  186.         JOIN
  187.             ReportLeg leg
  188.         ON
  189.             value.leg_id = leg.id
  190.        
  191.         JOIN
  192.             ReportLegPhase phase
  193.         ON
  194.             value.leg_phase_id = phase.id
  195.        
  196.         JOIN
  197.             ReportTime rtime
  198.         ON
  199.             value.time_id = rtime.id
  200.        
  201.         WHERE
  202.             value.voyage_id = @voyage_id AND
  203.             category_code = '-1' AND
  204.             value_code = 'SOG' AND
  205.             category_name = 'AUTO: Time Series' AND
  206.             phase.phase = 'SEA'
  207.  
  208.         ORDER BY
  209.             entry_time ASC
  210.    
  211.     FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  212. END
  213.  
  214. CLOSE voyage_id_cursor
  215. DEALLOCATE voyage_id_cursor
  216.  
  217.  
  218. DECLARE voyage_id_cursor CURSOR FOR
  219.     SELECT
  220.          voyage_id
  221.    
  222.     FROM
  223.         #temp_speed_profile
  224.  
  225. OPEN voyage_id_cursor
  226. FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  227.  
  228. WHILE @@FETCH_STATUS=0
  229. BEGIN
  230.     DECLARE temp_id_cursor CURSOR FOR
  231.         SELECT
  232.              temp_id,
  233.              speed_prev
  234.        
  235.         FROM
  236.             #temp_speed_profile
  237.        
  238.         WHERE
  239.             voyage_id = @voyage_id
  240.  
  241.     OPEN temp_id_cursor
  242.     FETCH NEXT FROM temp_id_cursor INTO @temp_id, @speed
  243.  
  244.  
  245.     SELECT @distance = 0
  246.     WHILE @@FETCH_STATUS=0
  247.     BEGIN
  248.         SELECT @distance = @distance + @speed * 600 -- in meters -------------------------------------------------------------
  249.  
  250.         UPDATE
  251.             #temp_speed_profile
  252.        
  253.         SET
  254.             distance = @distance
  255.        
  256.         WHERE
  257.             temp_id = @temp_id AND
  258.             voyage_id = @voyage_id
  259.            
  260.  
  261.         FETCH NEXT FROM temp_id_cursor INTO @temp_id, @speed
  262.     END
  263.  
  264.     CLOSE temp_id_cursor
  265.     DEALLOCATE temp_id_cursor
  266.  
  267.     FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  268. END
  269.  
  270. CLOSE voyage_id_cursor
  271. DEALLOCATE voyage_id_cursor
  272.  
  273.  
  274. -------------------^^^^^ get the data for the last n voyages in speed_prev ^^^^^^---------------------
  275.  
  276. DECLARE voyage_id_cursor CURSOR FOR
  277.     SELECT TOP 1
  278.          voyage_id
  279.    
  280.     FROM
  281.         #temp_voyage
  282.  
  283.  
  284. OPEN voyage_id_cursor
  285. FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  286.  
  287.  
  288. WHILE @@FETCH_STATUS=0
  289. BEGIN
  290.     INSERT INTO #temp_speed_profile
  291.             (voyage_id,
  292.              entry_time,
  293.              speed_new
  294.             )
  295.            
  296.         SELECT
  297.              voyage_id,
  298.              entry_time,
  299.              value.value
  300.        
  301.         FROM
  302.             ReportValue value
  303.        
  304.         JOIN
  305.             ReportValueType type
  306.         ON
  307.             value.type_id = type.id
  308.        
  309.         JOIN
  310.             ReportLeg leg
  311.         ON
  312.             value.leg_id = leg.id
  313.        
  314.         JOIN
  315.             ReportLegPhase phase
  316.         ON
  317.             value.leg_phase_id = phase.id
  318.        
  319.         JOIN
  320.             ReportTime rtime
  321.         ON
  322.             value.time_id = rtime.id
  323.        
  324.         WHERE
  325.             value.voyage_id = @voyage_id AND
  326.             category_code = '-1' AND
  327.             value_code = 'SOG' AND
  328.             category_name = 'AUTO: Time Series' AND
  329.             phase.phase = 'SEA'
  330.  
  331.         ORDER BY
  332.             entry_time ASC
  333.    
  334.     FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  335. END
  336.  
  337. CLOSE voyage_id_cursor
  338. DEALLOCATE voyage_id_cursor
  339.  
  340. GO
  341.  
  342.  
  343. DECLARE @voyage_id int
  344. DECLARE @distance decimal(18, 3)
  345. DECLARE @speed decimal(18, 3)
  346. DECLARE @temp_id int
  347.  
  348. DECLARE voyage_id_cursor CURSOR FOR
  349.     SELECT
  350.          voyage_id
  351.    
  352.     FROM
  353.         #temp_speed_profile
  354.  
  355.  
  356. OPEN voyage_id_cursor
  357. FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  358.  
  359.  
  360. WHILE @@FETCH_STATUS=0
  361. BEGIN
  362.     DECLARE temp_id_cursor CURSOR FOR
  363.         SELECT
  364.              temp_id,
  365.              speed_new
  366.        
  367.         FROM
  368.             #temp_speed_profile
  369.        
  370.         WHERE
  371.             voyage_id = @voyage_id
  372.  
  373.     OPEN temp_id_cursor
  374.     FETCH NEXT FROM temp_id_cursor INTO @temp_id, @speed
  375.  
  376.  
  377.     SELECT @distance = 0
  378.     WHILE @@FETCH_STATUS=0
  379.     BEGIN
  380.         SELECT @distance = @distance + @speed * 600 -- in meters   -----------------------------------------------------------
  381.  
  382.         UPDATE
  383.             #temp_speed_profile
  384.        
  385.         SET
  386.             distance = @distance
  387.        
  388.         WHERE
  389.             temp_id = @temp_id AND
  390.             voyage_id = @voyage_id
  391.  
  392.  
  393.         FETCH NEXT FROM temp_id_cursor INTO @temp_id, @speed
  394.     END
  395.  
  396.  
  397.     CLOSE temp_id_cursor
  398.     DEALLOCATE temp_id_cursor
  399.  
  400.     FETCH NEXT FROM voyage_id_cursor INTO @voyage_id
  401. END
  402.  
  403. CLOSE voyage_id_cursor
  404. DEALLOCATE voyage_id_cursor
  405.  
  406.  
  407. -------------------^^^^^ get the data for the last voyage in speed_prev ^^^^^^---------------------
  408.  
  409.  
  410. SELECT
  411.     temp_id,
  412.     voyage_id,
  413.     entry_time,
  414.     speed_prev * 1.943844 as "speed_prev", -- from m/s to knots
  415.     speed_new * 1.943844 as "speed_new", -- from m/s to knots
  416.     distance / 1852 as "distance" -- from meters to nautical miles
  417.  
  418. FROM
  419.     #temp_speed_profile
  420.  
  421.  
  422. DROP TABLE #temp_speed_profile
  423. DROP TABLE #temp_voyage
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement