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!

An uneccessarily lengthy query 1

Status
Not open for further replies.

jimny

Technical User
Joined
Oct 18, 2002
Messages
52
Location
US
There must be an easier way to get the same rsults from this lengthy query. I am trying to return the average of each column excluding rows that contain the value '0'. this is on ASP page, but the SQl is more my issue.

strSQL = "SELECT count(*) as Rows,sum(q1) as sq1,sum(q2) as sq2,sum(q3) as sq3" & _
",sum(q4) as sq4,sum(q5) as sq5,sum(q6) as sq6,sum(q7) as sq7,sum(q8) as sq8" & _
",sum(q9) as sq9,sum(q10) as sq10 FROM CofE03"
set rs = server.CreateObject("ADODB.RecordSet")
set rs = conn.Execute(strSQL)
dim RwCnt,sQ1,sQ2,sQ3,sQ4,sQ5,sQ6,sQ7,sQ8,sQ9,sQ10
Rwcnt = rs("Rows")
sQ1 = rs("sq1")
sQ2 = rs("sq2")
sQ3 = rs("sq3")
sQ4 = rs("sq4")
sQ5 = rs("sq5")
sQ6 = rs("sq6")
sQ7 = rs("sq7")
sQ8 = rs("sq8")
sQ9 = rs("sq9")
sQ10 = rs("sq10")
rs.Close
set rs = nothing
dim z1,z2,z3,z4,z5,z6,z7,z8,z9,z10
set rs = server.CreateObject("ADODB.RecordSet")
strSQl = "SELECT count(q1) as z1 FROM CofE03 WHERE q1 = '0'"
set rs = conn.Execute(strSQL)
z1 = rs("z1")
strSQl = "SELECT count(q2) as z2 FROM CofE03 WHERE q2 = '0'"
set rs = conn.Execute(strSQL)
z2 = rs("z2")
strSQl = "SELECT count(q3) as z3 FROM CofE03 WHERE q3 = '0'"
set rs = conn.Execute(strSQL)
z3 = rs("z3")
strSQl = "SELECT count(q4) as z4 FROM CofE03 WHERE q4 = '0'"
set rs = conn.Execute(strSQL)
z4 = rs("z4")
strSQl = "SELECT count(q5) as z5 FROM CofE03 WHERE q5 = '0'"
set rs = conn.Execute(strSQL)
z5 = rs("z5")
strSQl = "SELECT count(q6) as z6 FROM CofE03 WHERE q6 = '0'"
set rs = conn.Execute(strSQL)
z6 = rs("z6")
strSQl = "SELECT count(q7) as z7 FROM CofE03 WHERE q7 = '0'"
set rs = conn.Execute(strSQL)
z7 = rs("z7")
strSQl = "SELECT count(q8) as z8 FROM CofE03 WHERE q8 = '0'"
set rs = conn.Execute(strSQL)
z8 = rs("z8")
strSQl = "SELECT count(q9) as z9 FROM CofE03 WHERE q9 = '0'"
set rs = conn.Execute(strSQL)
z9 = rs("z9")
strSQl = "SELECT count(q10) as z10 FROM CofE03 WHERE q10 = '0'"
set rs = conn.Execute(strSQL)
z10 = rs("z10")
tQ1 = Round(sq1/(RwCnt - z1),2)
tQ2 = Round(sq2/(RwCnt - z2),2)
tQ3 = Round(sq3/(RwCnt - z3),2)
tQ4 = Round(sq4/(RwCnt - z4),2)
tQ5 = Round(sq5/(RwCnt - z5),2)
tQ6 = Round(sq6/(RwCnt - z6),2)
tQ7 = Round(sq7/(RwCnt - z7),2)
tQ8 = Round(sq8/(RwCnt - z8),2)
tQ9 = Round(sq9/(RwCnt - z9),2)
tQ10 = Round(sq10/(RwCnt - z10),2)
 
you can do it all in one query (runs faster)

select case when sum(
case when q1=0 then 0 else 1 end
) = 0
then 0
else sum(q1) / sum(
case when q1=0 then 0 else 1 end
)
end as avg1

repeat this pattern for the other columns

the "count" is a sum of 1's and 0's

when the "count" is 0, the average is set to 0 (to prevent dividing by 0), otherwise, the sum is divided the "count"


rudy
 
Thanx Rudy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top