Sql select most repeated value

M Simo 20 Reputation points
2023-02-08T13:31:04.7266667+00:00

Hi,

I have a database called a “reader”, and a table called "dailyreader". The dailyreader has 6 columns and i am looking to create a query to select the most repeated values in all columns and show how many times has been repeated.

Thanks,

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,370 questions
SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,707 questions
{count} votes

Accepted answer
  1. Viorel 112.1K Reputation points
    2023-02-08T13:36:49.93+00:00

    Try a query like this:

    select top(10) with ties 
       Column1, Column2, Column3, Column4, Column5, Column6, 
       count(*) as [how many times]
    from reader..dailyreader
    group by Column1, Column2, Column3, Column4, Column5, Column6
    order by [how many times] desc
    

    If you mean something different, then show details or examples.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. PercyTang-MSFT 12,426 Reputation points Microsoft Vendor
    2023-02-09T02:07:03.3266667+00:00

    Hi @M Simo

    If I understand correctly, you can try this query.

    ;with CTE as(
      select column1,column2,column3,column4,column5,column6,count(*) as nums,
             dense_rank()over(order by count(*) desc) as ord
      from reader.dailyreader group by column1,column2,column3,column4,column5,column6)
    select * from CTE where ord = 1;
    

    Best regards,

    Percy Tang


    If the answer is the right solution, please click "Accept Answer". If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Olaf Helper 40,816 Reputation points
    2023-02-09T06:22:37.3533333+00:00

    I have a database called a “reader”, and a table called "dailyreader"

    And now we have to guess the table design (DDL) and the existing values? No.

    select the most repeated values in all columns

    Repeating in which way? What for columns? What for data?

    0 comments No comments