-- getReloadEligbilityAgeCalculationErrors.sql -- -- Dennis Kelly -- Copyright (C) 2010 Dennis Kelly -- Copyright (C) 2010 Stevens Pass -- -- This program is free software; you can redistribute it and/or -- modify it under the terms of the GNU General Public License -- as published by the Free Software Foundation; either version 2 -- of the License, or (at your option) any later version. -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- You should have received a copy of the GNU General Public License -- along with this program; if not, write to the Free Software -- Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. DECLARE @TranStartDate DATETIME DECLARE @FixedDate DATETIME SET @TranStartDate = '2010-08-15' SET @FixedDate = '2010-12-31' CREATE TABLE #t ( IPCode INT, Mn INT, Age INT, Mx INT ) INSERT INTO #t SELECT tlip.IPCode, cr1.CompareValue, DATEDIFF(YY, pp.BirthDate, @FixedDate) - CASE WHEN( (MONTH(pp.BirthDate)*100 + DAY(pp.BirthDate)) > (MONTH(@FixedDate)*100 + DAY(@FixedDate)) ) THEN 1 ELSE 0 END, cr2.CompareValue FROM TransactionHeader th WITH(NOLOCK) JOIN TransactionLine tl WITH(NOLOCK) ON tl.TransactionID = th.TransactionID AND tl.LineStatusCode IN (1, 3) AND tl.LineTypeCode = 1 JOIN TransactionLineIP tlip WITH(NOLOCK) ON tlip.TransactionID = tl.TransactionID AND tlip.TransactionLine = tl.TransactionLine JOIN PersonProfile pp WITH(NOLOCK) ON tlip.IPCode = pp.IPCode AND pp.StatusCode = 1 AND pp.ProfileTypeCode = 1 JOIN CompareRule cr1 WITH(NOLOCK) ON cr1.EntityCode = 4 -- Line Item AND tl.ProductHeaderCode = cr1.EntityInstanceCode AND cr1.RuleCode = 25 -- Minimum age JOIN CompareRule cr2 WITH(NOLOCK) ON cr2.EntityCode = 4 -- Line Item AND tl.ProductHeaderCode = cr2.EntityInstanceCode AND cr2.RuleCode = 26 -- Maximum age WHERE th.TransactionTypeCode = 1 AND th.TransactionDate > @TranStartDate SELECT * FROM #t WHERE Age > Mx OR Age < Mn DROP TABLE #t