SQL - 从Users表中查询日志

时间:2014-06-23 18:49:29

标签: sql-server count distinct

基于以下示例:(它是一个" QueryLog"表,此表存储用户与两个不同产品N和R之间的交互):


Id         Date             UserID      Product
--------------------------------------------------
0  2013-06-09 14:50:24.000   100           N
1  2013-06-09 15:27:23.000   100           N
2  2013-06-09 15:29:23.000   100           N
3  2013-06-17 15:31:23.000   100           N
4  2013-06-17 15:32:23.000   100           N
5  2014-05-19 15:30:23.000   250           N
6  2014-07-19 15:27:23.000   250           N
7  2014-07-19 15:27:23.000   333           R
8  2014-08-19 15:27:23.000   333           R

预期结果:

Count
-----
1                      

(只有UserID 250符合我的标准)

如果一个用户仅在一个月内与该产品进行了10次互动,则他不符合我的标准。

要恢复,我正在寻找:

至少超过一个月与产品N进行互动的不同用户数(此用户在一个月内可能进行的互动次数)

这是我尝试过的代码:

select distinct v.UserID, v.mois , v.annee
from 
(select c.UserID , c. mois, c.annee, COUNT(c.UserID) as frequence

from

(
    SELECT 
    datepart(month,[DATE]) as mois,
    datepart(YEAR,[DATE]) as annee ,
    Username,
    UserID,
    Product

    FROM QueryLog 
    where Product = 'N'

 ) c

group by c.UserID, c.annee, c.mois
) v
group by v.UserID, v.mois, v.annee

2 个答案:

答案 0 :(得分:0)

试试这个:

DECLARE @YourTable table (Id int, [Date] datetime, UserID int, Product char(1))

INSERT INTO @YourTable VALUES (0,'2013-06-09 14:50:24',100 ,'N')
,(1,'2013-06-09 15:27:23',100 ,'N')
,(2,'2013-06-09 15:29:23',100 ,'N')
,(3,'2013-06-17 15:31:23',100 ,'N')
,(4,'2013-06-17 15:32:23',100 ,'N')
,(5,'2014-05-19 15:30:23',250 ,'N')
,(6,'2014-07-19 15:27:23',250 ,'N')
,(7,'2014-07-19 15:27:23',333 ,'R')
,(8,'2014-08-19 15:27:23',333 ,'R')

;WITH MultiMonthUsers AS
(
    select
        UserID
        FROM (select
                  UserID 
                  FROM @YourTable 
                  WHERE product='N' 
                  GROUP BY UserID, YEAR([Date]),MONTH([Date]) 
             )dt2
        GROUP BY UserID 
        HAVING COUNT(*)>1
)
SELECT COUNT(*) FROM MultiMonthUsers

根据行数和索引数量,这将运行缓慢。使用YEAR([Date]),MONTH([Date])将阻止任何索引使用。

答案 1 :(得分:0)

我认为这样做会有,但我需要一个更好的数据集来测试:

SELECT COUNT(*)
FROM (
    --roll all month/user records into single row
    SELECT UserID, datediff(month 0, [date]) As MonthGroup
    FROM QueryLog
    WHERE Product='N'
    GROUP BY datediff(month 0, [date]), UserId
) t
-- look for users with multiple rows
GROUP BY UserID
HAVING COUNT(UserID) > 1

似乎应该有一种方法可以进一步推广它,以避免需要嵌套选择。