Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:14:29+00:00 2026-05-28T14:14:29+00:00

Possible Duplicate: Combine multiple results in a subquery into a single comma-separated value Concat

  • 0

Possible Duplicate:
Combine multiple results in a subquery into a single comma-separated value
Concat groups in SQL Server

I want to be able to get the duplication’s removed

SELECT Count(Data) as Cnt, Id
FROM [db].[dbo].[View_myView]
Group By Data
HAVING Count(Data) > 1

In MySQL it was as simple as this:

SELECT Count(Data), group_concat(Id)
FROM View_myView
Group By Data
Having Cnt > 1

Does anyone know of a solution? Examples are a plus!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:14:30+00:00Added an answer on May 28, 2026 at 2:14 pm

    In SQL Server as of version 2005 and newer, you can use a CTE (Common Table Expression) with the ROW_NUMBER function to eliminate duplicates:

    ;WITH LastPerUser AS
    (
       SELECT 
           ID, UserID, ClassID, SchoolID, Created,
           ROW_NUMBER() OVER(PARTITION BY UserID ORDER BY Created DESC) AS 'RowNum'
       FROM dbo.YourTable
    )
    SELECT 
       ID, UserID, ClassID, SchoolID, Created,
    FROM LastPerUser
    WHERE RowNum = 1
    

    This CTE “partitions” your data by UserID, and for each partition, the ROW_NUMBER function hands out sequential numbers, starting at 1 and ordered by Created DESC – so the latest row gets RowNum = 1 (for each UserID) which is what I select from the CTE in the SELECT statement after it.

    Using the same CTE, you can also easily delete duplicates:

    ;WITH LastPerUser AS
    (
       SELECT 
           ID, UserID, ClassID, SchoolID, Created,
           ROW_NUMBER() OVER(PARTITION BY UserID ORDER BY Created DESC) AS 'RowNum'
       FROM dbo.YourTable
    )
    DELETE FROM dbo.YourTable t
    FROM LastPerUser cte
    WHERE t.ID = cte.ID AND cte.RowNum > 1
    

    Same principle applies: you “group” (or partition) your data by some criteria, you consecutively number all the rows for each data partition, and those with values larger than 1 for the “partitioned row number” are weeded out by the DELETE.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: Combine multiple Collections into a single logical Collection? I have a lot
Possible Duplicates: Simulating group_concat MySQL function in MS SQL Server 2005? Concat groups in
Possible Duplicate: Clearest way to combine two lists into a map (Java)? Given this:
Possible Duplicate: Need help with a SQL query that combines adjacent rows into a
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP Twitter API - How to pull in multiple users tweets? Is
Possible Duplicate: Can you call Directory.GetFiles() with multiple filters? Does it possible to get
Possible Duplicate: Combine Gyroscope and Accelerometer Data I have read a number of papers
Possible Duplicate: Are there any Git command to combine all our ugly commits together

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.