SELECT *
FROM xxxx
WHERE Account IN (
SELECT Account
FROM xxxx
GROUP BY Account
HAVING COUNT(Proceedure)>1
)
SQL Server Aggregate Query
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.
-
Tom Phillips 17,741 Reputation points
2020-08-21T16:07:46.693+00:00
2 additional answers
Sort by: Most helpful
-
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.
-
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