SQL Server Aggregate Query

SM 21 Reputation points
2020-08-21T14:12:02.877+00:00

I need to write a query that returns the ID of the items counted to find duplicates.

I have a Transaction table with Procedures and need to find accounts where COUNT(Procedure)>1 GROUP BY Account. That part I can do. I don't know how to return the primary key IDs of the records used in the COUNT.

Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,656 questions
0 comments No comments
{count} votes

Accepted answer
  1. Tom Phillips 17,741 Reputation points
    2020-08-21T16:07:46.693+00:00
    SELECT *
    FROM xxxx
    WHERE Account IN (
        SELECT Account
        FROM xxxx
        GROUP BY Account
        HAVING COUNT(Proceedure)>1
    )
    
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. tibor_karaszi@hotmail.com 4,311 Reputation points
    2020-08-21T16:02:23.79+00:00

    So you want to refer to detailed rows, lost by the aggregation? Than you can't use just one query. You can for instance join your GROUP BY query to another query that get the detailed data. No need to store the intermediate stuff in a temp table, you can use derived table or CTE. Without DDL and an executable example, it is about as far as we can go.

    0 comments No comments

  2. EchoLiu-MSFT 14,581 Reputation points
    2020-08-24T09:49:00.597+00:00

    Hi @SM ,
    I am very happy that your problem has been solved.
    If you have any question, please feel free to let me know.

    Best Regards
    Echo

    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.