25. august 2006 - 14:30Der er
21 kommentarer og 6 løsninger
Finde værdier i array
Har et 2 dimisionelt array der bliver dannet ud fra et recordset.
col 1 col 2 1 10 1 12 1 14 1 16 1 9 2 10 2 12 1 13
Tal i col 1 kan være fra 1 og op efter.
Jeg skal ind i arrayet og ligge alle tal i col2 sammen hvor col1 = 1 også bagefter hvor col1 = 2 og bagefter hvor col1 = 3 hvis altså 3 findes i col1. osv osv
Bagefter eller samtidig skal de sammenregnede tal skrives ud.
function sumi(id, byVar arrayname) summa = 0 for i=0 to ubound( arrayname ) if( arrayname(i,0) = id) then summa = summa + arrayname(i,1) end if next sumi = summa end Function
function sumi(id, byVal arrayname) summa = 0 for i=0 to ubound( arrayname ) if( arrayname(i,0) = id) then summa = summa + arrayname(i,1) end if next sumi = summa end Function
NB. Dette ER løsningen... men jeg vil hellere hjælpe dig med din SQL...
function sumi(id, byVal arrayname) summa = 0 for i=0 to ubound( arrayname ) if( arrayname(i,0) = id) then summa = summa + arrayname(i,1) end if next sumi = summa end Function
maxtal = 500 ' col1s' højeste værdi du nogensinde kommer ud for 'arr kan udskiftes med det array du allerede har lavet for a=0 to maxtal if( sumi(a,arr) > 0 )then response.write "Ved tal "&a&" er summen = " & sumi(a,arr) & "<br>" end if next
function sumi(id, byVal myArray2) summa = 0 for i=0 to ubound(myArray2) if( myArray2(i,0) = id) then summa = summa + myArray2(i,1) end if next sumi = summa end Function
maxtal = 500 ' col1s' højeste værdi du nogensinde kommer ud for 'arr kan udskiftes med det array du allerede har lavet for a=0 to maxtal if( sumi(a,arr) > 0 )then response.write "Ved tal "&a&" er summen = " & sumi(a,arr) & "<br>" end if next
Får følgende fejl Type mismatch: 'UBound'
Vi kan også godt prøve med sql'en
Har en sql sætning der bliver kørt x antal gange med forskellige prametre alt efter hvad brugeren vælger:
Hvis den bliver kørt 4 gange ser den således ud (forkortet version da den henter fra mange tabeller):
sql=select * from table 1 where S_G_Id = 117 and S_Year = 2006 And S_Month BETWEEN 1 AND 12
sql=select * from table 1 where S_G_Id = 117 and S_Year = 2007 And S_Month BETWEEN 1 AND 12
sql=select * from table 1 where S_G_Id = 117 and S_Year = 2008 And S_Month BETWEEN 1 AND 12
sql=select * from table 1 where S_G_Id = 117 and S_Year = 2009 And S_Month BETWEEN 1 AND 4
Løsning 1: ------------------------------------------------ Function sumi(id, byVal arr) summa = 0 for i=0 to ubound(arr) if( arr(i,0) = id) then summa = summa + arr(i,1) end if next sumi = summa end Function
maxtal = 500
for a=0 to maxtal if( sumi(a,myArray2) > 0 )then response.write "Ved tal "&a&" er summen = " & sumi(a,myArray2) & "<br>" end if next ------------------------------------------------
Jeg skal vide hvad kolonne navnene er på dine tabeller...
men eller kan du modde følgende:
<% sql_1 = select * from table 1 where S_G_Id = 117 and S_Year = 2006 And S_Month BETWEEN 1 AND 12 sql_2 = select * from table 1 where S_G_Id = 117 and S_Year = 2007 And S_Month BETWEEN 1 AND 12 sql_3 = select * from table 1 where S_G_Id = 117 and S_Year = 2008 And S_Month BETWEEN 1 AND 12 sql_4 = select * from table 1 where S_G_Id = 117 and S_Year = 2009 And S_Month BETWEEN 1 AND 4
DAS_MEGA_SQL = "Select col1, sum(col2) from ((" DAS_MEGA_SQL = DAS_MEGA_SQL & sql_1 & ") union all (" DAS_MEGA_SQL = DAS_MEGA_SQL & sql_2 & ") union all (" DAS_MEGA_SQL = DAS_MEGA_SQL & sql_3 & ") union all (" DAS_MEGA_SQL = DAS_MEGA_SQL & sql_4 & ")) group by col1 order by col1 asc" %>
Function sumi(id, byVal arr) summa = 0 for i=0 to UBound(arr,2) '<--- ændret if( arr(i,0) = id) then summa = summa + arr(i,1) end if next sumi = summa end Function
Kører den her sql sætning x antal gange og ligger værdierne ind i arrayet.
SQL = "SELECT Reports.R_Year, Statistic.S_G_Id, Statistic.S_G_Soldunits, Statistic.S_G_Price, Statistic.S_Month, Games.G_Gamename, Distributor.D_Company, Reports.R_Invoicenumber, Valuta.V_Valutaname, Valuta.V_Id FROM Valuta INNER JOIN ((Distributor INNER JOIN Reports ON Distributor.D_Id = Reports.R_D_Id) INNER JOIN (Games INNER JOIN Statistic ON Games.G_Id = Statistic.S_G_Id) ON Reports.R_Id = Statistic.S_R_Id) ON Valuta.V_Id = Reports.R_Valuta WHERE (((Reports.R_Year)=" & FirstYear & ") AND ((Statistic.S_G_Id)=" & Request.form("Selected_Game") & ") AND ((Statistic.S_Month) Between " & Month1 & " And " & Month2 & ")) order by Valuta.V_Id asc"
Rs.Open Sql,conn,1,2 If not rs.eof Then While not rs.eof SQL2 = "Select * from Valuta where V_Id = " & Rs("V_Id") Rs2.Open Sql2,conn,1,2 myArray2(0,a) = Rs("V_Id") myArray2(1,a) = Rs("S_G_Price") a = a + 1 Rs.Movenext Rs2.Close wend End if
Skriver værdierne ud med følgende
Response.Write "<table border='0'>" For i = 0 to UBound(myArray2,2) Response.Write "<tr><td>" & i & "</td>" Response.Write "<td>" & myArray2(0,i) & "</td>" Response.Write "<td>" & myArray2(1,i) & "</td>" Next Response.Write "</table>"
<% Function sumi(id, byVal arr) summa = 0 for i=0 to ubound(arr) if( arr(i,0) = id) then summa = summa + arr(i,1) end if next sumi = summa end Function
Dim myArray2 Redim myArray2(d,1)
SQL = "SELECT Reports.R_Year, Statistic.S_G_Id, Statistic.S_G_Soldunits, Statistic.S_G_Price, Statistic.S_Month, Games.G_Gamename, Distributor.D_Company, Reports.R_Invoicenumber, Valuta.V_Valutaname, Valuta.V_Id FROM Valuta INNER JOIN ((Distributor INNER JOIN Reports ON Distributor.D_Id = Reports.R_D_Id) INNER JOIN (Games INNER JOIN Statistic ON Games.G_Id = Statistic.S_G_Id) ON Reports.R_Id = Statistic.S_R_Id) ON Valuta.V_Id = Reports.R_Valuta WHERE (((Reports.R_Year)=" & FirstYear & ") AND ((Statistic.S_G_Id)=" & Request.form("Selected_Game") & ") AND ((Statistic.S_Month) Between " & Month1 & " And " & Month2 & ")) order by Valuta.V_Id asc"
Rs.Open Sql,conn,1,2 If not rs.eof Then While not rs.eof SQL2 = "Select * from Valuta where V_Id = " & Rs("V_Id") Rs2.Open Sql2,conn,1,2 myArray2(a,0) = Rs("V_Id") myArray2(a,1) = Rs("S_G_Price") a = a + 1 Rs.Movenext Rs2.Close wend End if
maxtal = 500
for a=0 to maxtal if( sumi(a,myArray2) > 0 )then response.write "Ved tal " & a & " er summen = " & sumi(a,myArray2) & "<br>" end if next
Response.Write "<table border='0'>" For j = 0 to 500 tmp = 0 found = false For i = 0 to UBound(myArray2,2) if myArray2(0,i) = j Then found = true tmp = tmp + myArray2(1,i) end if Next if found then i = i + 1 Response.Write "<tr><td>" & i & "</td>" Response.Write "<td>" & j & "</td>" Response.Write "<td>" & tmp & "</td>" end if Next Response.Write "</table>"
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.