J.J.
J.J.

Reputation: 1118

How to step through ASP classic for loop backwards?

I am retrieving only the 10 most recent results from this query, which is then causing them to be sorted from newest to oldest, but I want them to show from oldest to newest instead. I tried doing this with a subselect query, but couldn't properly retrieve the results when doing that. Now I am attempting to do this by stepping backwards through loops like so

for i=ubound(arr,2) to 0 step -1

This is still going through the array from 0 to the last element, as the chart that is being created is still showing the descending date elements. How can I fix this, or is there a better way to go about this? Thanks!

Dim data()
    Redim data(doccount)
    label=""
    strSQL = "Select TOP 10 DATEPART(m, thedate),DATEPART(d, thedate),DATEPART(yyyy, thedate),DATEPART(hh, thedate),DATEPART(mi, thedate), servicestatus from [ServiceUptime] order by thedate desc"
    'Response.write(strSQL)
    Set rs = objConnection.Execute(strSQL, ,adCmdText)
    if not (rs.bof or rs.eof) then
        arr = rs.getrows()
        ''Create Data set
        for i=0 to ubound(arr,2)
            if i = ubound(arr,2) then
                for j=0 to doccount
                    data(j) = data(j) &"["& i &","& arr(j+5,i) &"]"
                next
            else
                for j=0 to doccount
                    data(j) = data(j) &"["& i &","& arr(j+5,i) &"],"
                next
            end if
        next
        ''Create x axis Labels
        if ubound(arr,2)>9999 then
            ''There are too many points
            ''Attempt to only use 5
            Response.write(ubound(arr,2))
            unit = Round(ubound(arr,2)/5,0)
            for c = 0 to unit*5 Step unit
                label = label   &"["& c &",'"& monthArr(arr(0,c)) &" "& arr(1,c) &", "& arr(2,c) &", "& arr(3,c) &"':"& arr(4,c) &"'],"
            next

        else
            ''There are not enough points.
            ''Use them all

            for i=ubound(arr,2) to 0 step -1
                if i = 0 then
                    label = label   &"["& i &",'"& monthArr(arr(0,i)) &" "& arr(1,i) &" "& arr(2,i) &" "& arr(3,i) &":"& arr(4,i) &"']"
                else
                    label = label   &"["& i &",'"& monthArr(arr(0,i)) &" "& arr(1,i) &", "& arr(2,i) &" "& arr(3,i) &":"& arr(4,i) &"'],"
                end if
            next
        end if

        ''Create mouse over labels
        moLabel = "moArr = new Array(" & ubound(arr,2) & ");"
        for i=ubound(arr,2) to 0 step -1
            moLabel = moLabel & "moArr[" & i & "]='" & monthArr(arr(0,i)) &" "& arr(1,i) &", "& arr(2,i) &", "& arr(3,i) &":"& arr(4,i) &"';"
        next
    else
        dateNow = Date
        dateYest = DateAdd("d",-1,dateNow)
        for j=0 to doccount
            data(j) =  "[0,0],[1,0]"
        next
        label = "[0,'"&monthArr(DatePart("m",dateNow))&" "&DatePart("d",dateNow)&", "&DatePart("yyyy",dateNow)&"'][1,'"&monthArr(DatePart("m",dateYest))&" "&DatePart("d",dateYest)&", "&DatePart("yyyy",dateYest)&"']"
        moLabel = "moArr = new Array(2); moArr[0]='" & monthArr(DatePart("m",dateNow))&" "&DatePart("d",dateNow)&", "&DatePart("yyyy",dateNow) &"';"& "moArr[0]='" & monthArr(DatePart("m",dateYest))&" "&DatePart("d",dateYest)&", "&DatePart("yyyy",dateYest) &"';"
    end if

Upvotes: 0

Views: 905

Answers (1)

krlzlx
krlzlx

Reputation: 5822

Try with a subquery:

SELECT TOP 10 * FROM (
   SELECT DATEPART(m, thedate),DATEPART(d, thedate),DATEPART(yyyy, thedate),DATEPART(hh, thedate),DATEPART(mi, thedate), servicestatus 
   FROM [ServiceUptime] 
   ORDER BY thedate DESC)

Upvotes: 2

Related Questions