[英]Display all the table records whose count is greater than 1
我有一张表,其类别和品牌如下:
| id| category | brand | date | ........
| 1 | A | A1 | 12-DEC-09 | ........
| 1 | A | A1 | 12-DEC-09 |........
| 1 | A | A1 | 13-DEC-09 |........
| 2 | A | A2 | 14-DEC-09 |........
| 2 | B | B1 | 14-DEC-09 |........
| 2 | B | B2 | 14-DEC-09 |........
| 2 | B | B3 | 14-DEC-09 |........
我需要显示品牌超过 1 的结果。品牌基于类别。
我需要这样的结果:
| 1 | A | A1 | 12-DEC-09 | ......
| 1 | A | A1 | 12-DEC-09 |........
| 1 | A | A1 | 13-DEC-09 |........
我只能通过查询获得类别、品牌及其总数。
select
category,
brand,
count(*) as total
from
tbl_category
group by category, brand
但我需要上面的结果。
您可以使用 window 函数:
select c.*
from (select c.*, count(*) over (partition by category, brand) as cnt
from tbl_category c
) c
where cnt > 1;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.