我需要接受MAX计数,但它不起作用, 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;
答案 0 :(得分:2)
您需要将原始结果集中的行与获得最大使用量的同一结果集上的另一个查询进行匹配。
为清晰起见,我们将使用WITH
条款:
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 ;
现在,如果有多个tag_variable_type_id
达到最大值,则会为一个token_id
获得多行;如果你只需要一个,你就必须添加任意条件。
答案 1 :(得分: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:还有其他方法可以在没有分析函数的情况下编写查询,但这种方式可能是效率最高的
答案 2 :(得分: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
答案 3 :(得分: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的行。