我有一个 SQL 登录表“tblLogins”,它存储每个登录用户的 UserId 和 LoginDate。我需要生成一个图表来显示每天有多少登录。所以我在 [CLASSIC ASP] 下面使用这个脚本。
有没有办法在没有循环和每天查询数据库的情况下获取这些信息?如您所见,我正在使用 30 个查询访问数据库。预期的输出格式是这样的:
[[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]]
这是[每月的一天,登录]。我正在查看 SQL Server 中的 COALESCE,但不确定它是否可行并且不太了解其用法。
编码:
    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 
编辑:某些日子可能没有登录,因此结果应该在该月的那一天的结果中报告 0。@alzaimar 很接近,但那里仍然存在一些问题。