only work off of actual sales, not quotes
This commit is contained in:
		
							parent
							
								
									3675b8541f
								
							
						
					
					
						commit
						11b1a3a2e4
					
				| @ -51,39 +51,57 @@ BEGIN | |||||||
| 
 | 
 | ||||||
| 	-- Use the same selection logic as before | 	-- Use the same selection logic as before | ||||||
| 	-- 1. Prefer the most recent of dss/dsq if either is within the age threshold | 	-- 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) | 	IF (@dss_date IS NOT NULL AND @dss_date > @age_threshold) | ||||||
| 		OR (@dss_date IS NOT NULL AND @dss_date > @age_threshold) |     BEGIN | ||||||
| 	BEGIN |         INSERT INTO @result VALUES (@dss_price, 'dss', @dss_date, @dss_qty, @dss_dataseg, @dss_ord, @dss_quote, @dss_part); | ||||||
| 		IF @dsq_date IS NOT NULL AND (@dss_date IS NULL OR @dsq_date >= @dss_date) AND @dsq_date > @age_threshold |         RETURN; | ||||||
| 			INSERT INTO @result VALUES (@dsq_price, 'dsq', @dsq_date, @dsq_qty, @dsq_dataseg, @dsq_ord, @dsq_quote, @dsq_part); |     END | ||||||
| 		ELSE IF @dss_date IS NOT NULL AND @dss_date > @age_threshold | 	-- IF (@dsq_date IS NOT NULL AND @dsq_date > @age_threshold) | ||||||
| 			INSERT INTO @result VALUES (@dss_price, 'dss', @dss_date, @dss_qty, @dss_dataseg, @dss_ord, @dss_quote, @dss_part); | 	-- 	OR (@dss_date IS NOT NULL AND @dss_date > @age_threshold) | ||||||
| 		RETURN; | 	-- BEGIN | ||||||
| 	END | 	-- 	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 | 	-- 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) |     IF @mrs_date IS NOT NULL AND @mrs_date > @age_threshold | ||||||
| 	BEGIN |     BEGIN | ||||||
| 		IF @mrq_date IS NOT NULL AND (@mrs_date IS NULL OR @mrq_date >= @mrs_date) |         INSERT INTO @result VALUES (@mrs_price, 'mrs', @mrs_date, @mrs_qty, @mrs_dataseg, @mrs_ord, @mrs_quote, @mrs_part); | ||||||
| 			INSERT INTO @result VALUES (@mrq_price, 'mrq', @mrq_date, @mrq_qty, @mrq_dataseg, @mrq_ord, @mrq_quote, @mrq_part); |         RETURN; | ||||||
| 		ELSE IF @mrs_date IS NOT NULL |     END | ||||||
| 			INSERT INTO @result VALUES (@mrs_price, 'mrs', @mrs_date, @mrs_qty, @mrs_dataseg, @mrs_ord, @mrs_quote, @mrs_part); | 	-- IF (@mrq_date IS NOT NULL OR @mrs_date IS NOT NULL) | ||||||
| 		RETURN; | 	-- BEGIN | ||||||
| 	END | 	-- 	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 | 	-- 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; | 	-- DECLARE  | ||||||
| 	IF @dsq_date IS NOT NULL | 	--        @best_price NUMERIC(20,5) = 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; | 	--        ,@best_source NVARCHAR(10) = NULL | ||||||
| 	IF @dss_date IS NOT NULL AND (@best_date IS NULL OR @dss_date > @best_date) | 	--        ,@best_date DATE = NULL | ||||||
| 		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; | 	--        ,@best_qty NUMERIC(20,5) = NULL | ||||||
| 	IF @mrq_date IS NOT NULL AND (@best_date IS NULL OR @mrq_date > @best_date) | 	--        ,@best_dataseg NVARCHAR(100) = NULL | ||||||
| 		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; | 	--        ,@best_ord NVARCHAR(20) = NULL | ||||||
| 	IF @mrs_date IS NOT NULL AND (@best_date IS NULL OR @mrs_date > @best_date) | 	--        ,@best_quote NVARCHAR(20) = NULL | ||||||
| 		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; | 	--        ,@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 | 	-- 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); | 	-- 	INSERT INTO @result VALUES (@best_price, @best_source, @best_date, @best_qty, @best_dataseg, @best_ord, @best_quote, @best_part); | ||||||
| 
 | 
 | ||||||
| 	RETURN; | 	RETURN; | ||||||
| END | END | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user