简体   繁体   English

Oracle SQL查询获取计数

[英]Oracle SQL query Getting count of counts

I have a table of different films and every film has some copies: 我有一张不同电影的表格,每部电影都有一些副本:

inventory_id  film_id   store_id
     1           1         1
     2           1         1
     3           2         1
     4           2         1

I need to count the frequency of every film and count the amount of different films taht also have the same amount of copies, Like: 我需要计算每部电影的频率,并计算不同电影的数量,也可以有相同数量的副本,如:

Copies    Counts
  2         2

I wrote this: 我写了这个:

select DISTINCT COUNT(*) as Copies, 
       (select COUNT(*) from (select COUNT(*) as Copies from inventory
           group by film_id
           having store_id = 1)
       group by Copies
) as Counts from inventory
where store_id = 1
group by film_id
order by Copies DESC

And the result ist like: 结果就像:

Copies     Counts
   4         73
   3         73
   2         73 

I don't understand why the Counts all have the same value. 我不明白为什么Counts都具有相同的值。 How can I do it right? 我该怎么办?

Based on this description: 基于此描述:

I need to count the frequency of every film and count the amount of different films that also have the same amount of copies 我需要计算每部电影的频率,并计算同样数量的不同电影的数量

you would seem to want a histogram of histograms query: 你似乎想要直方图查询的直方图:

select copies, count(*) as num_films, min(film_id), max(film_id) 
from (select film_id, count(*) as copies
      from inventory
      group by film_id
     ) f
group by cnt
order by cnt;

The following query is doing something quite different, including filtering by store. 以下查询正在做一些完全不同的事情,包括按商店过滤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM