Link to home
Start Free TrialLog in
Avatar of pepper777
pepper777Flag for United States of America

asked on

How can I perform a variance on two fields and utilize that result in a where clause.

I'm trying to do something like this:
DECLARE
      @date AS INT,
      @prior AS INT
SELECT
      @date = 6757,
      @prior = 6575

SELECT
      a.countydimid, a.State, a.County, a.planname,  
      a.TotalLives, b.TotalLives
FROM
      dbo.vMMSCountyManagedMarketShare AS a
      INNER JOIN dbo.vMMSCountyManagedMarketShare AS b
            ON b.dailytimedimid = @Prior
            AND b.planid = a.planid
            AND b.countydimid = a.countydimid
WHERE
      a.dailytimedimid = @date
      AND a.planid IS NOT NULL
      AND Var(a.TotalLives, b.TotalLives) > .05
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Try this as:
DECLARE 
      @date AS INT,
      @prior AS INT
SELECT 
      @date = 6757,
      @prior = 6575
 
SELECT 
      a.countydimid, a.State, a.County, a.planname,  
      a.TotalLives, b.TotalLives
FROM 
      dbo.vMMSCountyManagedMarketShare AS a
      INNER JOIN dbo.vMMSCountyManagedMarketShare AS b 
            ON b.dailytimedimid = @Prior 
            AND b.planid = a.planid 
            AND b.countydimid = a.countydimid
WHERE 
      a.dailytimedimid = @date 
      AND a.planid IS NOT NULL
      AND ABS(a.TotalLives - b.TotalLives) > .05

Open in new window

Avatar of pepper777

ASKER

The .05 is a percentage:

AND Var(a.TotalLives, b.TotalLives) > .05%
pepper777,

Try this then.  Variance should be the difference in the two numbers as a percentage of first number, correct?  Therefore, I wrapped with absolute value so it can be +/- .05%.

mwvisa1
DECLARE 
      @date AS INT,
      @prior AS INT
SELECT 
      @date = 6757,
      @prior = 6575
 
SELECT 
      a.countydimid, a.State, a.County, a.planname,  
      a.TotalLives, b.TotalLives
FROM 
      dbo.vMMSCountyManagedMarketShare AS a
      INNER JOIN dbo.vMMSCountyManagedMarketShare AS b 
            ON b.dailytimedimid = @Prior 
            AND b.planid = a.planid 
            AND b.countydimid = a.countydimid
WHERE 
      a.dailytimedimid = @date 
      AND a.planid IS NOT NULL
      AND ABS((a.TotalLives - b.TotalLives) * 1.0/a.TotalLives) > .05

Open in new window

We have already tried that and that will function for most of the values except for those which are zero. SQL will throw an error for a.TotalLives if it is equal to zero and we need to be able to account for those values which are equal to zero.
If you have zero's in data then do this:
DECLARE 
      @date AS INT,
      @prior AS INT
SELECT 
      @date = 6757,
      @prior = 6575
 
SELECT 
      a.countydimid, a.State, a.County, a.planname,  
      a.TotalLives, b.TotalLives
FROM 
      dbo.vMMSCountyManagedMarketShare AS a
      INNER JOIN dbo.vMMSCountyManagedMarketShare AS b 
            ON b.dailytimedimid = @Prior 
            AND b.planid = a.planid 
            AND b.countydimid = a.countydimid
WHERE 
      a.dailytimedimid = @date 
      AND a.planid IS NOT NULL
      AND CASE a.TotalLives WHEN 0 THEN 0 ELSE ABS((a.TotalLives - b.TotalLives) * 1.0/a.TotalLives) END > .05

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial