简体   繁体   中英

Display all the table records whose count is greater than 1

I have a table which has category and brand as follows:

| 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 |........

I need to display the result with brand more than 1. The brand is based on category.

I need result like this:

| 1 |   A      | A1    | 12-DEC-09 | ......
| 1 |   A      | A1    | 12-DEC-09 |........
| 1 |   A      | A1    | 13-DEC-09 |........

I am only get category, brand and its total number with my query.

select
    category,
    brand,
    count(*) as total
from
    tbl_category
group by category, brand

But I need the result like above.

You can use window functions:

select c.*
from (select c.*, count(*) over (partition by category, brand) as cnt
      from tbl_category c
     ) c
where cnt > 1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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