像STATS_MODE这样的Oracle统计信息可以避免将默认行为扩展到另一列

时间:2017-01-27 16:23:30

标签: sql oracle oracle10g

查询从日期列表中提取最受欢迎的小时。

表: ID PARENT_ID 开始 持续时间

static const uint8_t PROGMEM letter_data[][8] =
{
  {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 032 ( )
  {0x00, 0x00, 0x2E, 0x00, 0x00, 0x00, 0x00, 0x00}, // Char 033 (!)
  {0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00} // Char 034 (")
}

for(j = 0; j <= messageLength - 1; j++){    //Go through each character in the message.
  int Character = message[j] - 32;    // first visible ASCIIcharacter '!' is number 33. reads and stores the ASCII value of the current Character we are dealing with and -32 so the char correspnds to our array.
  printer.printBitmap(letter_width, letter_height, letter_data[Character]);
}

这很好用但是如果我们有相同提取(小时)的记录,STATS_MODE将默认采用最小值。

相反,在没有唯一结果的边缘情况下,我想使用持续时间来扩展过滤器。

E.g。

select STATS_MODE(extract(HOUR from started)) as most_pop_call_start, 
       avg(duration) as avg_duration 
from table where parent_id = 'xxx';

在这种情况下,我希望将most_pop_call_start值设置为'13',因为提取(HOUR)的最大计数(*)组返回2且多于1组记录有2作为计数,因此我将评估持续时间列以79为最大子集(10,11,12,13)。

2 个答案:

答案 0 :(得分:0)

尝试:

select min( hr ) keep ( dense_rank last order by cnt,max_duration ) as most_pop_call_start, 
       sum( duration ) / sum( cnt ) as avg_duration 
from (
    select extract(HOUR from started) as hr,
           count(*)  as cnt,
           sum( duration ) as duration,
           max( duration )  as max_duration
    from table
    where parent_id = 'xxx'
    group by extract(HOUR from started)
);

答案 1 :(得分:0)

我从krokodilko提案开始解决。

这是整个查询:

UITextField

非常感谢