|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Hi all
I'm using select count(*) in MySQL statements to return total records. I have 2 of these recordsets. I'm using response.write to show the results on the asp page. I'd like to calculate a percentage using the 2 results eg(sqlResult1/sqlResult2 *100) errors are occuring. Is there a tried and true method of doing this. I appreciate any advice
__________________
Damo |
|
#2
|
||||
|
||||
|
Quote:
is it a logical error...or a syntax error..../ post relevant code
__________________
Please give respect to those that helped solve an issue by clicking on the reputation icon
|
|
#3
|
|||
|
|||
|
It's a type mismatch error. I've tried assigning variable types... but that doesn't seem to work...
below are the recordsets... <% set rs=Server.CreateObject("ADODB.recordset") rs.Open "Select count(*) From LogIn Where FirstLogin='1' and State = 'Queensland'",conn response.write(rs.Fields(0)) LoginQueensland=rs.Fields(0) rs.close%> <% set rsQP=Server.CreateObject("ADODB.Recordset") rsQP.Open "Select Count(*) From LogIn where State='Queensland'", conn rsQP=rsQP.Fields(0) %> <% repsonse.write (LoginQueensland/rsQP * 100) %> Thanks ... |
|
#4
|
||||
|
||||
|
Hi,
rsQP is a recordset You should declare a variable like you did for LoginQueensland Let say declare LoginTotalQueensland Try this Hope it helps |
|
#5
|
||||
|
||||
|
Code:
<%
set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select count(*) AS Q1 From LogIn Where FirstLogin='1' and State = 'Queensland'",conn
iQ1 = rs("Q1")
rs.close
set rsQP=Server.CreateObject("ADODB.Recordset")
rsQP.Open "Select Count(*) AS Q2 From LogIn where State='Queensland'", conn
iQ2 = rsQP("Q2")
If iQ1 > 0 AND iQ2 > 0 Then
' String
Response.Write "(" & iQ1 & "/" & iQ2 & "* 100)"
' Result
Response.Write CDbl(iQ1/iQ2 * 100)
End If
%>
Last edited by keep_it_simple : May 12th, 2008 at 10:05 PM. Reason: mispelled vars and changed result to cdbl |
|
#6
|
|||
|
|||
|
I'll give it a go ....
|
|
#7
|
||||
|
||||
|
Quote:
indeed...good eye..ty |
|
#8
|
|||
|
|||
|
Hi all
Nope... type mismatch errors are occuring. The method works if only one variable is used in the final response.write.... but when dividing (or multiplying for that matter) errors occur. Perhaps its because the select count(*) result isn't really a number??? I can't think why else mismatch errors would occur?? |
|
#9
|
||||
|
||||
|
not many people know, but VBScript has data types.
when you bring something from database, it might have data type that VBScript can't handle by default. for such cases, you can use special functions that will convert data to the desired type. one of those is CLng, which stand for Cast Long. correct syntax would be: Code:
<%
Set rs=Server.CreateObject("ADODB.recordset")
rs.Open "Select count(*) From LogIn Where FirstLogin='1' and State = 'Queensland'",conn
FirstLoginQueensland = CLng(rs(0))
rs.close
Set rsQP=Server.CreateObject("ADODB.Recordset")
rsQP.Open "Select Count(*) From LogIn where State='Queensland'", conn
LoginQueensland = CLng(rsQP(0))
rsQP.Close
response.write("first: " & FirstLoginQueensland & "<br />")
response.write("total: " & LoginQueensland & "<br />")
response.write("percentage: " & ((FirstLoginQueensland/LoginQueensland) * 100) & "<br />")
%>
|
|
#10
|
|||
|
|||
|
Nice work shadow wizard. Thanks to the others as well.
Just to finish off... how do I display the calculation to 2 decimal places?? Thanks again |
|
#11
|
|||
|
|||
|
shadow wizard
i've tried looking up vbscript data types... without any luck. do you have a handy URL reference somewhere that i could refer to if i have future data type issues?? Thanks again |
|
#12
|
|||
|
|||
|
I found a data type reference that may be useful
http://www.microsoft.com/technet/sc...s.mspx?mfr=true I'm still having trouble with rounding to 2 decimal places... |
|
#13
|
||||
|
||||
|
|
|
#14
|
||
|