Forum Discussion
GarthVader
Feb 22, 2024Copper Contributor
How to get the top occurrence of rows to show SSMS 19
I am struggling to get the most occurring rows to show in SSMS 19. ChatGPT gave me: SELECT TOP 1 Product, COUNT(*) AS Occurrences FROM Sales WHERE Product IS NOT NULL GROUP BY Product ORDER ...
olafhelper
Feb 22, 2024Bronze Contributor
GarthVader , the more correct query would be
SELECT TOP 1 SUB.Product, SUB.Occurrences
(SELECT Product, COUNT(*) AS Occurrences
FROM Sales
WHERE Product IS NOT NULL
GROUP BY Product) AS SUB
ORDER BY SUB.Occurrences DESC;
Or use ROW_NUMBER (Transact-SQL) - SQL Server | Microsoft Learn
- GarthVaderFeb 22, 2024Copper Contributor
Thanks olafhelper I wasn't able to get it work unfortunately.