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,

Developer technologies .NET Other
SQL Server Other
{count} votes

Accepted answer
  1. Viorel 122.5K 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. Anonymous
    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 47,436 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

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.