pepper777
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.vMMSCountyManagedMarke tShare AS a
INNER JOIN dbo.vMMSCountyManagedMarke tShare 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
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.vMMSCountyManagedMarke
INNER JOIN dbo.vMMSCountyManagedMarke
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
ASKER
The .05 is a percentage:
AND Var(a.TotalLives, b.TotalLives) > .05%
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
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
ASKER
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window