diff --git a/procs/lastprice_logic.ms.sql b/procs/lastprice_logic.ms.sql index fbde84c..6c8b408 100644 --- a/procs/lastprice_logic.ms.sql +++ b/procs/lastprice_logic.ms.sql @@ -51,39 +51,57 @@ BEGIN -- Use the same selection logic as before -- 1. Prefer the most recent of dss/dsq if either is within the age threshold - IF (@dsq_date IS NOT NULL AND @dsq_date > @age_threshold) - OR (@dss_date IS NOT NULL AND @dss_date > @age_threshold) - BEGIN - IF @dsq_date IS NOT NULL AND (@dss_date IS NULL OR @dsq_date >= @dss_date) AND @dsq_date > @age_threshold - INSERT INTO @result VALUES (@dsq_price, 'dsq', @dsq_date, @dsq_qty, @dsq_dataseg, @dsq_ord, @dsq_quote, @dsq_part); - ELSE IF @dss_date IS NOT NULL AND @dss_date > @age_threshold - INSERT INTO @result VALUES (@dss_price, 'dss', @dss_date, @dss_qty, @dss_dataseg, @dss_ord, @dss_quote, @dss_part); - RETURN; - END + IF (@dss_date IS NOT NULL AND @dss_date > @age_threshold) + BEGIN + INSERT INTO @result VALUES (@dss_price, 'dss', @dss_date, @dss_qty, @dss_dataseg, @dss_ord, @dss_quote, @dss_part); + RETURN; + END + -- IF (@dsq_date IS NOT NULL AND @dsq_date > @age_threshold) + -- OR (@dss_date IS NOT NULL AND @dss_date > @age_threshold) + -- BEGIN + -- IF @dsq_date IS NOT NULL AND (@dss_date IS NULL OR @dsq_date >= @dss_date) AND @dsq_date > @age_threshold + -- INSERT INTO @result VALUES (@dsq_price, 'dsq', @dsq_date, @dsq_qty, @dsq_dataseg, @dsq_ord, @dsq_quote, @dsq_part); + -- ELSE IF @dss_date IS NOT NULL AND @dss_date > @age_threshold + -- INSERT INTO @result VALUES (@dss_price, 'dss', @dss_date, @dss_qty, @dss_dataseg, @dss_ord, @dss_quote, @dss_part); + -- RETURN; + -- END -- 2. If both dss/dsq are older than the threshold, use the most recent of mrs/mrq if either exists - IF (@mrq_date IS NOT NULL OR @mrs_date IS NOT NULL) - BEGIN - IF @mrq_date IS NOT NULL AND (@mrs_date IS NULL OR @mrq_date >= @mrs_date) - INSERT INTO @result VALUES (@mrq_price, 'mrq', @mrq_date, @mrq_qty, @mrq_dataseg, @mrq_ord, @mrq_quote, @mrq_part); - ELSE IF @mrs_date IS NOT NULL - INSERT INTO @result VALUES (@mrs_price, 'mrs', @mrs_date, @mrs_qty, @mrs_dataseg, @mrs_ord, @mrs_quote, @mrs_part); - RETURN; - END + IF @mrs_date IS NOT NULL AND @mrs_date > @age_threshold + BEGIN + INSERT INTO @result VALUES (@mrs_price, 'mrs', @mrs_date, @mrs_qty, @mrs_dataseg, @mrs_ord, @mrs_quote, @mrs_part); + RETURN; + END + -- IF (@mrq_date IS NOT NULL OR @mrs_date IS NOT NULL) + -- BEGIN + -- IF @mrq_date IS NOT NULL AND (@mrs_date IS NULL OR @mrq_date >= @mrs_date) + -- INSERT INTO @result VALUES (@mrq_price, 'mrq', @mrq_date, @mrq_qty, @mrq_dataseg, @mrq_ord, @mrq_quote, @mrq_part); + -- ELSE IF @mrs_date IS NOT NULL + -- INSERT INTO @result VALUES (@mrs_price, 'mrs', @mrs_date, @mrs_qty, @mrs_dataseg, @mrs_ord, @mrs_quote, @mrs_part); + -- RETURN; + -- END -- 3. If all are at least as old as the threshold, pick the least oldest price available - DECLARE @best_price NUMERIC(20,5) = NULL, @best_source NVARCHAR(10) = NULL, @best_date DATE = NULL, @best_qty NUMERIC(20,5) = NULL, @best_dataseg NVARCHAR(100) = NULL, @best_ord NVARCHAR(20) = NULL, @best_quote NVARCHAR(20) = NULL, @best_part NVARCHAR(100) = NULL; - IF @dsq_date IS NOT NULL - SELECT @best_price = @dsq_price, @best_source = 'dsq', @best_date = @dsq_date, @best_qty = @dsq_qty, @best_dataseg = @dsq_dataseg, @best_ord = @dsq_ord, @best_quote = @dsq_quote, @best_part = @dsq_part; - IF @dss_date IS NOT NULL AND (@best_date IS NULL OR @dss_date > @best_date) - SELECT @best_price = @dss_price, @best_source = 'dss', @best_date = @dss_date, @best_qty = @dss_qty, @best_dataseg = @dss_dataseg, @best_ord = @dss_ord, @best_quote = @dss_quote, @best_part = @dss_part; - IF @mrq_date IS NOT NULL AND (@best_date IS NULL OR @mrq_date > @best_date) - SELECT @best_price = @mrq_price, @best_source = 'mrq', @best_date = @mrq_date, @best_qty = @mrq_qty, @best_dataseg = @mrq_dataseg, @best_ord = @mrq_ord, @best_quote = @mrq_quote, @best_part = @mrq_part; - IF @mrs_date IS NOT NULL AND (@best_date IS NULL OR @mrs_date > @best_date) - SELECT @best_price = @mrs_price, @best_source = 'mrs', @best_date = @mrs_date, @best_qty = @mrs_qty, @best_dataseg = @mrs_dataseg, @best_ord = @mrs_ord, @best_quote = @mrs_quote, @best_part = @mrs_part; + -- DECLARE + -- @best_price NUMERIC(20,5) = NULL + -- ,@best_source NVARCHAR(10) = NULL + -- ,@best_date DATE = NULL + -- ,@best_qty NUMERIC(20,5) = NULL + -- ,@best_dataseg NVARCHAR(100) = NULL + -- ,@best_ord NVARCHAR(20) = NULL + -- ,@best_quote NVARCHAR(20) = NULL + -- ,@best_part NVARCHAR(100) = NULL; + -- IF @dsq_date IS NOT NULL + -- SELECT @best_price = @dsq_price, @best_source = 'dsq', @best_date = @dsq_date, @best_qty = @dsq_qty, @best_dataseg = @dsq_dataseg, @best_ord = @dsq_ord, @best_quote = @dsq_quote, @best_part = @dsq_part; + -- IF @dss_date IS NOT NULL AND (@best_date IS NULL OR @dss_date > @best_date) + -- SELECT @best_price = @dss_price, @best_source = 'dss', @best_date = @dss_date, @best_qty = @dss_qty, @best_dataseg = @dss_dataseg, @best_ord = @dss_ord, @best_quote = @dss_quote, @best_part = @dss_part; + -- IF @mrq_date IS NOT NULL AND (@best_date IS NULL OR @mrq_date > @best_date) + -- SELECT @best_price = @mrq_price, @best_source = 'mrq', @best_date = @mrq_date, @best_qty = @mrq_qty, @best_dataseg = @mrq_dataseg, @best_ord = @mrq_ord, @best_quote = @mrq_quote, @best_part = @mrq_part; + -- IF @mrs_date IS NOT NULL AND (@best_date IS NULL OR @mrs_date > @best_date) + -- SELECT @best_price = @mrs_price, @best_source = 'mrs', @best_date = @mrs_date, @best_qty = @mrs_qty, @best_dataseg = @mrs_dataseg, @best_ord = @mrs_ord, @best_quote = @mrs_quote, @best_part = @mrs_part; - IF @best_price IS NOT NULL - INSERT INTO @result VALUES (@best_price, @best_source, @best_date, @best_qty, @best_dataseg, @best_ord, @best_quote, @best_part); + -- IF @best_price IS NOT NULL + -- INSERT INTO @result VALUES (@best_price, @best_source, @best_date, @best_qty, @best_dataseg, @best_ord, @best_quote, @best_part); RETURN; END