Martin Nikolaev Ivanov Martin Nikolaev Ivanov - 1 day ago 4
SQL Question

Sort a result in groups

I have a table with multiple duplicates by name for different users.I get the duplicates in the table using a query like

SELECT t1.*
FROM [Database].[dbo].[table] t1
INNER JOIN
( SELECT [Name]
FROM [Database].[dbo].[table]
GROUP BY [Name]
HAVING COUNT(*) >1 ) t2 ON t1.Name = t2.Name


From this I get the duplicates by name from the table. My question is this, how do I get the results sorted in groups like so

Name Customer

John Daydream
John Daydream
Peter Daydream
Peter Daydream
Peter Daydream
Amber Fly_co
Amber Fly_co
Jackie Fly_co
Jackie Fly_co

Answer
        SELECT t1.*
        FROM [Database].[dbo].[table] t1
        INNER JOIN
          (SELECT [Name]
                   FROM [Database].[dbo].[table]
                   GROUP BY [Name]  
                   HAVING COUNT(*) >1 ) t2 ON t1.Name = t2.Name
 ORDER BY t1.Customer, t1.Name
Comments