Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query % change using IIF first time 1

Status
Not open for further replies.

qwertyth

Technical User
Jan 26, 2010
6
US
Hello,
I have a table with two fields(text) X, Y, both have 20 entries of yes, no.

I am trying to find the percent change in them and am running into trouble if Sum(X='yes')=0 or Sum(Y='yes')=0 or Sum(X='yes')/Sum(Y='yes')=1.

I need

IF Sum(X)=0 and Sum(Y)=0 then %change=0
IF Sum(X)<>0 and Sum(Y)=0 then %change=Sum(X)*100
IF Sum(X)=0 and Sum(Y)<>0 then %change=Sum(Y)*100
IF Sum(X)<>0 and Sum(Y)<>0 then %change=Sum(Y)/Sum(X)
IF Sum(Y)/Sum(X)=1 then %change=0

I used the IIF function for the first time, it seems to work?, dose this look right? , is there a better way to go about this.

Code:
SELECT IIF( Sum(Abs(Y ='Yes'))<>0 and Sum(Abs(X = 'Yes'))<>0 AND Sum(Abs(Y ='Yes'))/Sum(Abs(X ='Yes'))<>1 , Sum(Abs(Y ='Yes'))/Sum(Abs(X ='Yes')),IIF( Sum(Abs(Y ='Yes'))/Sum(Abs(X ='Yes'))=1,0,Sum(Abs(Y ='Yes')) + Sum(Abs(Y ='Yes'))) AS [% Change]
FROM tblFlash4;

Thanks for any insight.
 
Since I just want the %change regardles of Sum(Abs(Y ='Yes'))>Sum(Abs(X ='Yes'))or Sum(Abs(Y ='Yes'))<Sum(Abs(X ='Yes')) the above needs to be

Code:
SELECT IIF( Sum(Abs(Y ='Yes'))<>0 and Sum(Abs(X = 'Yes'))<>0 AND Sum(Abs(Y ='Yes'))/Sum(Abs(X ='Yes'))<>1 ,IIF(Sum(Abs(Y='Yes'))>Sum(Abs(X='Yes')),  Sum(Abs(Y='Yes'))/Sum(Abs(X='Yes'),  Sum(Abs(X='Yes'))/Sum(Abs(Y='Yes'))), IIF( Sum(Abs(Y ='Yes'))/Sum(Abs(X ='Yes'))=1,0,Sum(Abs(Y ='Yes')) + Sum(Abs(Y ='Yes'))) AS [% Change]
FROM tblFlash4;
 




Hi,

Not knowing exactly what this table represents, I have some doubts. Let me pose a different kind of example and then you tell me where your data is of a different kind.

Yesterday I HAD $100.
Today I HAVE $75.
% Change = ([Yesterday] - [Today]) / [Yesterday] = (100-75)/100 = -25




Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thank you Skip that helped clarify my thinking, my head was spinning around when I wrote the above.

I finality ended up with.

Code:
SELECT IIf(Sum(Abs(PM29='Yes'))<>0 And Sum(Abs(PM29='Yes'))/Sum(Abs(PM19='Yes'))<>0,Sum(Abs(PM29='Yes'))/Sum(Abs(PM19='Yes'))-1,Sum(Abs(PM29='Yes'))-Sum(Abs(PM19='Yes'))) AS [% Change]
FROM tblFlash4;
/Code]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top