user881807
user881807

Reputation: 23

Limit the number of duplicate rows in SQL query result

I have a sql query that returns duplicate rows. I want refine the result so that the duplicate rows are limited to 5. How can I do this ?

Upvotes: 1

Views: 497

Answers (1)

Error_2646
Error_2646

Reputation: 3801

This will work pretty generally, but it assumes your DBMS supports ROW_NUMBER(). You should tag your DBMS, better solutions may be possible ... or this one might not even work.

SELECT All,
       of, 
       your,
       columns     
  FROM (SELECT T1.*
               ROW_NUMBER() OVER
                 ( PARTITION BY All,
                                Of,
                                Your,
                                Columns
                 ) AS rn
          FROM Table1 T1
       ) TMP
 WHERE rn <= 5;

Upvotes: 1

Related Questions