2

我需要回答的问题是“我们在 60 分钟内收到的最大页面请求数是多少?”

我有一个看起来与此类似的表:

date_page_requested      date;
page                     varchar(80);

我正在寻找任何 60 分钟时间片中的最大行数。

我认为分析函数可能会让我到达那里,但到目前为止我还处于空白状态。

我想要一个正确方向的指针。

4

5 回答 5

2

您在答案中有一些可行的选择,这是使用 Oracle 的“具有逻辑偏移的窗口函数”功能而不是连接或相关子查询的选项。

首先是测试表:

Wrote file afiedt.buf

  1  create table t pctfree 0 nologging as
  2  select date '2011-09-15' + level / (24 * 4) as date_page_requested
  3  from dual
  4* connect by level <= (24 * 4)
SQL> /

Table created.

SQL> insert into t values (to_date('2011-09-15 11:11:11', 'YYYY-MM-DD HH24:Mi:SS'));

1 row created.

SQL> commit;

Commit complete.

T 现在在一天中每一刻钟包含一行,并在上午 11:11:11 增加一行。查询分三个步骤进行。第 1 步是,对于每一行,获取在该行时间之后的下一小时内出现的行数:

  1  with x as (select date_page_requested
  2          , count(*) over (order by date_page_requested
  3              range between current row
  4                  and interval '1' hour following) as hour_count
  5      from t)

然后按 hour_count 分配排序:

  6  , y as (select date_page_requested
  7          , hour_count
  8          , row_number() over (order by hour_count desc, date_page_requested asc) as rn
  9      from x)

最后选择具有最多后续行数的最早行。

 10  select to_char(date_page_requested, 'YYYY-MM-DD HH24:Mi:SS')
 11      , hour_count
 12  from y
 13* where rn = 1

如果多个 60 分钟的窗口与小时计数并列,则上面只会给您第一个窗口。

于 2011-09-17T07:27:14.080 回答
0

对于 PostgreSQL,我首先可能会为按分钟对齐的“窗口”编写类似的内容。为此,您不需要 OLAP 窗口函数。

select w.ts, 
       date_trunc('minute', w.ts) as hour_start, 
       date_trunc('minute', w.ts) + interval '1' hour as hour_end,
       (select count(*) 
        from weblog 
        where ts between date_trunc('minute', w.ts) and 
                        (date_trunc('minute', w.ts) + interval '1' hour) ) as num_pages
from weblog w
group by ts, hour_start, hour_end
order by num_pages desc

Oracle 也有一个 trunc() 函数,但我不确定格式。我要么马上查一下,要么去看朋友的滑稽表演。

于 2011-09-16T22:17:53.193 回答
0

这应该给你你需要的东西,返回的第一行应该有页数最多的小时。

select number_of_pages
      ,hour_requested
from (select to_char(date_page_requested,'dd/mm/yyyy hh') hour_requested
            ,count(*) number_of_pages
      from pages
      group by to_char(date_page_requested,'dd/mm/yyyy hh')) p
order by number_of_pages
于 2011-09-16T21:25:30.847 回答
0

这样的事情怎么样?

SELECT TOP 1
       ranges.date_start,
       COUNT(data.page) AS Tally
  FROM (SELECT DISTINCT 
               date_page_requested AS date_start,
               DATEADD(HOUR,1,date_page_requested) AS date_end
          FROM @Table) ranges
  JOIN @Table data
    ON data.date_page_requested >= ranges.date_start
   AND data.date_page_requested < ranges.date_end
 GROUP BY ranges.date_start
 ORDER BY Tally DESC
于 2011-09-16T21:30:29.873 回答
0
WITH ranges AS
    ( SELECT
          date_page_requested          AS StartDate,
          date_page_requested + (1/24) AS EndDate,
          ROWNUMBER() OVER(ORDER BY date_page_requested) AS RowNo 
      FROM 
          @Table
    ) 

SELECT 
    a.StartDate                 AS StartDate,
    MAX(b.RowNo) - a.RowNo + 1  AS Tally 
FROM 
    ranges a
  JOIN
    ranges b 
      ON  a.StartDate <= b.StartDate
      AND b.StartDate < a.EndDate
GROUP BY a.StartDate
       , a.RowNo
ORDER BY Tally DESC

或者:

WITH ranges AS
    ( SELECT
          date_page_requested          AS StartDate,
          date_page_requested + (1/24) AS EndDate,
          ROWNUMBER() OVER(ORDER BY date_page_requested) AS RowNo 
      FROM 
          @Table
    ) 

SELECT 
    a.StartDate                      AS StartDate,
    ( SELECT MIN(b.RowNo) - a.RowNo  
      FROM ranges b
      WHERE b.StartDate > a.EndDate
    )                                AS Tally
FROM 
    ranges a
ORDER BY Tally DESC
于 2011-09-16T22:27:13.647 回答