表中前10个出现的值

时间:2011-12-14 00:18:58

标签: sql sql-server sql-server-2008 distinct-values

我正在尝试整理一个SQL查询,以收集过去一周内排名前10位的新闻。 我还需要它来过滤看过同一个新闻项目的重复IP地址。

每次用户进入页面时,都会获取用户的浏览器查询字符串 以下是数据库设置的一个示例:

datetime   | ipaddress     | querystring
-----------------------------------------
9/12/2011  | 65.65.65.651  | newsid=3512
9/12/2011  | 65.65.65.658  | newsid=3512
10/12/2011 | 65.65.65.653  | newsid=3514
11/12/2011 | 65.65.65.656  | newsid=3515
11/12/2011 | 65.65.65.651  | newsid=3515
13/12/2011 | 65.65.65.651  | newsid=3516
14/12/2011 | 65.65.65.650  | newsid=3516
14/12/2011 | 65.65.65.650  | newsid=3516

我失败的尝试:

 SELECT DISTINCT TOP 10 ipaddress, querystring, Count(*) AS thecount
      FROM [thedb].[dbo].[tblwebstats] 
      WHERE querystring LIKE '%newsid=%' AND datetime > (1 week ago)
      GROUP BY querystring, ipaddress
      ORDER BY Count(*) DESC

请帮帮我:)。

3 个答案:

答案 0 :(得分:1)

这样的事情怎么样?

select top 10 querystring, count(querystring) as popularity
from 
(
    select distinct ipaddress, querystring
    from 
    (
        select [datetime], ipaddress, querystring
        from tblwebstats
        where querystring LIKE '%newsid=%' AND [datetime] > dateadd(day, -7, getdate())
    ) as datefilter
) as distinctfilter
group by querystring
order by popularity desc

此查询执行以下操作(从最里面到最外面):

  1. 根据需要按日期范围和查询字符串过滤原始表格
  2. 将结果从(1)减少到不同的(IP地址,查询字符串)对,忽略日期
  3. 计算(2)中唯一的查询字符串出现次数,并按计数降序返回前10位。

答案 1 :(得分:0)

我假设你说“...过滤重复的IP地址......”你只想要从同一个IP地址请求的同一篇新闻文章计算一次(每天?)

如果是这样,您需要在获取文章之前过滤重复项,请尝试以下方法:

WITH Unique_Requests AS ( 
    SELECT DISTINCT datetime, ipaddress, querystring
    FROM [thedb].[dbo].[tblwebstats]
    WHERE datetime >= DATEADD(week, -1, CURRENT_TIMESTAMP) AND
        querystring LIKE '%newsid=%'
)

SELECT TOP 10 querystring, Count(*) AS thecount
FROM Unique_Requests
GROUP BY querystring
ORDER BY Count(*) DESC

答案 2 :(得分:0)

对以下声明不满意。嵌入太多而不占用临时表。如果将数据放入其他临时表中,则交叉应用将更轻。

DECLARE @t as table(Created datetime,IPAddress varchar(15),QueryString VARCHAR(20))

INSERT INTO @t(Created,IPAddress,QueryString) VALUES
('2012-11-9' ,'65.65.65.651' ,'newsid=3512' ),
('2012-11-9','65.65.65.658','newsid=3512'),
('2012-11-10','65.65.65.653','newsid=3514'),
('2011-12-11','65.65.65.656','newsid=3515'),
('2011-12-11','65.65.65.651','newsid=3515'),
('2011-12-13','65.65.65.651','newsid=3516'),
('2011-12-14','65.65.65.650','newsid=3516'),
('2011-12-14','65.65.65.650','newsid=3516')

SELECT TOP 10 QueryString,DistinctIp,COUNT(1) Counter FROM (
SELECT DISTINCT Created,IPAddress,DistinctIp,QueryString
FROM @t t
CROSS APPLY (SELECT DISTINCT COUNT(1) DistinctIp FROM @t WHERE Created = t.Created ANd QueryString = t.QueryString) g
WHERE Created >= CAST((GETDATE()-7) AS DATE) AND
    QueryString LIKE '%newsid=%'
) x
GROUP BY QueryString,DistinctIp 
ORDER BY Counter DESC

该语句的结果将包含额外的不同IP地址计数。

QueryString|DistinctIp|Counter
newsid=3515|2|2
newsid=3512|2|2
newsid=3516|2|1
newsid=3516|1|1
newsid=3514|1|1