I have an SQL Logins table "tblLogins" which stores the UserId and the LoginDate for each user that logs in. I need to generate a graph to show how many logins were there daily. So I use this script below [CLASSIC ASP].
Is there any way to get this info without the loop and querying database for each day? As you can see, I am hitting the DB with 30 queries. OUTPUT FORMAT expected is this:
[[1, 26], [2, 16], [3, 16], [4, 26], [5, 0], [6, 0], [7, 25], [8, 21], [9, 90], [10, 12], [11, 11], [12, 21], [13, 0], [14, 18], [15, 17], [16, 21], [17, 23], [18, 19], [19, 0], [20, 0], [21, 12], [22, 17], [23, 12], [24, 7], [25, 11], [26, 21], [27, 0], [28, 18], [29, 20], [30, 0]]
which is [day of month, logins]. I was looking at COALESCE in SQL Server but not sure its possible and don't know the usage well.
THE CODE:
    i=1
    varStartDate =  DateAdd("m",-1, Date) 
    for i = 1 to DayCount
    intUserTotal = 0
    strSQL= "Select IsNull(Count(DISTINCT(userid)),0) as Logins from tblLogins where LoginDate >= '"& varStartDate &"' and LoginDate <= '"& DateAdd("d", 1, varStartDate) &"'
    rsTemp.Open strSQL,objConn,3,2
    if not rsTemp.EOF then
        if not isnull(rsTemp("Logins")) and trim(rsTemp("Logins")) <> "" then
          intUserTotal = trim(rsTemp("Logins"))
        else
          intUserTotal = 0
        end if
    else
        intUserTotal = 0
    end if
    rsTemp.close
    '' append in JSON FORMAT
    strData1= strData1 & "[" & i & ", "  & cInt(intUserTotal) & "], "
    ''' increment the date
    varStartDate =  DateAdd("d",1, varStartDate) 
    '' start with next date
    next 
EDIT: There may be NO logins on some days so result should report 0 in the result for that day of the month. @alzaimar is close but there are still some problems there.
 
     
    