Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Getting the MAX() of COUNT() aggregates

$
0
0
Hello,I've been searching on how to do this but am coming up empty. I am sure there is a simple, elegant solution, but my brain is not seeing it. I have a query:[quote]SELECT d.state, vs.modelID As Model, sum(vs.totalSalesCount) As TotalCarsSoldFROM Dealerships d inner join VehicleSales vs on vs.dealershipID = d.dealershipID inner join Vehicles v on vs.modelID = v.modelIDWHERE v.vehicleClass = 'SUV'GROUP BY d.state, vs.modelIDORDER BY d.state, vs.modelID[/quote]And I need to get the [b]bestselling SUV model[/b] for each [b]state[/b]. The result set should return 50 records (one per state). [img]http://content.screencast.com/users/ccaswell7487/folders/Snagit/media/0e94efe4-cf91-4282-8e39-57f1e5e809ea/2016-02-07_12-40-39.png[/img]

Viewing all articles
Browse latest Browse all 3145

Trending Articles