1

我需要计算我的最大数量,但它不起作用,Oracle 不允许深度聚合..

这是没有 MAX 的结果:

187 1   2
187 3   1
159 1   1
159 3   1
159 2   8
188 2   9
188 1   2
188 3   1
187 2   9



select tt.token_id, tt.tag_variable_type_id, max(count(*)) as usage
from tokens tt
left outer join token_xref ttx on ttx.token_id = tt.token_id
where tag_variable_type_id in (1, 2, 3) and ttx.template_id = 52
group by tt.token_id, tt.tag_variable_type_id

我也试着把它放在 max(count(*)); 但没有工作

有没有办法在一个查询中获得 COUNT() 的 MAX 而没有内部选择

我期待在MAX之后

187 2 9
159 2 8
188 2 9

编辑

我还有一个问题是使用分析查询,而不是仅仅猜测如果我有重复要排除哪一行

MIN(tag_variable_type_id) KEEP (DENSE_RANK LAST ORDER BY usage) AS tag_variable_type_id

我使用解码函数分配给我的变量类型“优先级”,所以我的猜测是 MIN(priority) KEEP (DENSE_RANK LAST ORDER BY usage) AS priority,请参阅查询

但我又失去了我的 tag_variable_type_id..

无论如何要保留它?我把它解码回来,但可能是更好的方法

 decode (MIN(priority) KEEP (DENSE_RANK LAST ORDER BY usage), 1,2, 2,1, 3,3) as typevar,



select token_id,    
   MIN(priority) KEEP (DENSE_RANK LAST ORDER BY usage) AS priority,
   MAX(usage) AS usage 
from ( select tt.token_id, tt.tag_variable_type_id,  
              decode(
                tt.tag_variable_type_id,
                1, 2, 
                2, 1, 
                3, 3  
              ) as priority,
              count(*) as usage 
       from tag_tokens tt 
       left outer join template_token_xref ttx on ttx.token_id = tt.token_id 
       where tag_variable_type_id in (1, 2, 3) and ttx.template_id = 52 
       group by tt.token_id, tt.tag_variable_type_id)
group by token_id;
4

4 回答 4

2

You need to match lines from your original result set with another query on the same result set that gets the max of usage.

We'll be using a WITH clause for clarity :

WITH result_count as (select tt.token_id, tt.tag_variable_type_id, count(*) as usage
    from tokens tt
       left outer join token_xref ttx on ttx.token_id = tt.token_id
       where tag_variable_type_id in (1, 2, 3) and ttx.template_id = 52
       group by tt.token_id, tt.tag_variable_type_id )
select result_count.token_id, tag_variable_type_id, max_usage
   from result_count   join  -- original result set
     (select token_id, max(usage) max_usage
        from result_count
        group by token_id) result_max on  -- result set with max usage
         result_count.token_id = result_max.token_id AND
         usage = max_usage ;

Now, if there are several tag_variable_type_id that reach the max, you'll get several lines for one token_id ; if you only need one you'll have to add an arbitrary condition.

于 2013-09-05T08:46:37.723 回答
1

尝试

select token_id, 
   MIN(tag_variable_type_id) KEEP (DENSE_RANK LAST ORDER BY usage) AS tag_variable_type_id,
   MAX(usage) AS usage from ( select tt.token_id, tt.tag_variable_type_id, count(*) as usage from tokens tt left outer join token_xref ttx on ttx.token_id = tt.token_id where tag_variable_type_id in (1, 2, 3) and ttx.template_id = 52 group by tt.token_id, tt.tag_variable_type_id ) group by token_id ;

注意1:MIN(tag_variable_type_id)不一定正确,我只是假设它在功能上是多余的,只有Oracle SQL语法需要

注意2:还有其他编写查询的方法没有分析功能,但这种方式可能是最有效的

于 2013-09-05T01:37:31.237 回答
0

ROW_NUMBER可用于为每一行分配唯一的行号。

select token_id,tag_variable_type_id,usage
  from(
       select tt.token_id token_id,
              tt.tag_variable_type_id tag_variable_type_id,
              count(*) as usage,
              row_number() over (partition by tt.token_id order by count(*) desc) as r
         from tokens tt left outer join token_xref ttx
              on ttx.token_id = tt.token_id
        where tag_variable_type_id in (1, 2, 3)
          and ttx.template_id = 52
        group by tt.token_id, tt.tag_variable_type_id
        )
 where r= 1;
  • order by count(*) desc确保最高计数获得第 1 行和次高第 2 行,依此类推。
  • partition by tt.token_id确保为每组 token_id 重置此编号。

在外部查询中使用此行号仅过滤行号为 1 的行。

于 2013-09-05T04:31:22.957 回答
0

所以你只需要围绕你已经拥有的东西包装另一个选择:

select token_id, max(usage) from (
 select tt.token_id, tt.tag_variable_type_id, max(count(*)) as usage
 from tokens tt
 left outer join token_xref ttx on ttx.token_id = tt.token_id
 where tag_variable_type_id in (1, 2, 3) and ttx.template_id = 52
 group by tt.token_id, tt.tag_variable_type_id
) 
group by token_id
于 2013-09-05T01:14:18.037 回答