报告每天,每月或每年列出新旧用户的访问次数

时间:2012-05-31 11:00:34

标签: mysql sql sql-server-2008 ms-access

我有2个表格,包含以下表格结构 -

用户 UserID int,名称 varchar(50), DateCreated 日期时间)和

UserLogin Id int, UserId int, DateLoggedIn 日期时间)

我需要按照以下格式生成每天/每月/每年登录应用程序的用户的报告 -

日期,NewUsers,OldUsers,TotalUsers

日期 - 为报告选择的日期

NewUser - 如果用户首次在应用程序中登录,则他将被视为新用户,否则将被视为旧用户。

OldUser - 首次访问后的后续用户登录。

例如,如果用户A在15日登录一个月,则他将在15日被视为新用户,但作为旧用户在网站上进行后续访问并需要在报告中报告为旧用户。同样,如果用户B在30/4/2012登录一次,那么他将被视为旧用户。

你们可以给我一个查询来输出报告吗?

我的数据如下 -

USER表

| UserID |         Name | DateCreated |
|--------|--------------|-------------|
|      1 |      Rajveer |  2012-01-01 |
|      2 |  Manoj Tyagi |  2011-08-10 |
|      3 | Raghu Pandey |  2009-05-13 |
|      4 |   Bhajan Lal |  2012-05-01 |
|      5 | Satbir Singh |  2011-08-03 |

USERLOGIN表

| ID | UserID | DateLoggedIn |
|----|--------|--------------|
|  1 |      1 |   2012-04-27 |
|  2 |      1 |   2012-05-25 |
|  3 |      1 |   2012-05-28 |
|  4 |      2 |   2012-03-21 |
|  5 |      2 |   2012-05-30 |
|  6 |      3 |   2012-05-20 |
|  7 |      4 |   2012-05-29 |
|  8 |      5 |   2012-04-22 |
|  9 |      3 |   2012-05-15 |

我确实需要一个月的整个数据(因此不需要明确)....理想情况下,根据我提供的数据,我应该得到以下

| Month | NewUsers | OldUsers | TotalUsers |
|-------|----------|----------|------------|
|     3 |        1 |        0 |          1 |
|     4 |        2 |        0 |          2 |
|     5 |        2 |        4 |          6 |

由于用户ID 3在may中登录了两次,因此他的第一次访问应被视为新访问,第二次访问应视为旧访问。

TIA,

Paritosh Arya。

2 个答案:

答案 0 :(得分:0)

您可以在联接日期在登录表和users表之间执行外部联接,然后计算每个表中的不同用户ID:

SELECT   DATE(l.DateLoggedIn)                                AS Date,
         COUNT(DISTINCT u.UserID)                            AS NewUsers,
         COUNT(DISTINCT l.UserId) - COUNT(DISTINCT u.UserID) AS OldUsers,
         COUNT(DISTINCT l.UserId)                            AS TotalUsers
FROM     UserLogin                                           AS l
  LEFT JOIN Users                                            AS u
         ON l.UserId = u.UserID
        AND DATE(l.DateLoggedIn) = DATE(u.DateCreated)
GROUP BY Date

答案 1 :(得分:0)

我认为这显示了逻辑,但可能没有最高的性能。

    ;With NewUser as (
        Select ul.UserID
          , Min(ul.DateLoggedIn) as NewDate
        from dbo.USERLOGIN as ul
        group by UserID)

    Select 
      Month(l.DateLoggedIn) as ThisMonth
      , (Select Count(*) from dbo.USERLOGIN as ul 
         inner join NewUser as nu 
         on ul.UserID = nu.UserID
              and ul.DateLoggedIn = nu.NewDate 
         where Month(ul.DateLoggedIn) = Month(l.DateLoggedIn)
        ) as NewUsers
      , (Select Count(*) from dbo.USERLOGIN as ul 
         where Month(ul.DateLoggedIn) = Month(l.DateLoggedIn)
            AND not exists (Select * from NewUser as nu
             where ul.UserID = nu.UserID
                            and ul.DateLoggedIn = nu.NewDate)
        ) as OldUsers
      , Count(*) as TotalUsers
    from dbo.USERLOGIN as l
    group by Month(l.DateLoggedIn)

-- a little testing code
 CREATE TABLE [dbo].[USERLOGIN](
        [ID] [int] NOT NULL,
        [USERID] [int] NOT NULL,
        [DateLoggedIn] [date] NOT NULL
    )
    ;
    insert INTO dbo.USERLOGIN 

    select 1, 1, '2012-04-27'
    union all
    Select 2, 1, '2012-05-25'
    union all
    Select  3 , 1,'2012-05-28'
    union all
    Select 4 ,  2,'2012-03-21'
    union all
    Select 5 , 2,'2012-05-30'
    union all
    Select 6 , 3 ,'2012-05-20'
    union all
    Select 7 , 4 ,'2012-05-29'
    union all
    Select 8 , 5,'2012-04-22'
    union all
    Select  9 , 3,'2012-05-15'