Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
hi all, i have a table where users are filling in product reviews,and 1 field "RECOMMENDED USE" is a Varchar type and can have values like "DAY,EVENING, or NIGHT."now i want to know what most users have selected,a simple query will like Select Top 1 RecommendedUse,Count(*) ReviewCount From ProductReviews Where ProductID = 1 Group By RecommendedUse Order By ReviewCountwill give me what i need,but i have more then 1 field that i need the most selected,(like RecommendedUse,RecommendedAge,and so on) and i don't want to make sub-query for every fieldso whats the best way to do this?
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts
Posted - 2011-05-12 : 13:35:40
Can you do something like this?
SELECT MAX(UseCount), MAX(AgeCount), MAX(SomethingElseCount)FROM( SELECT COUNT(*) OVER (PARTITION BY RecommendedUse) AS UseCount, COUNT(*) OVER (PARTITION BY RecommendedAge) AS AgeCount, COUNT(*) OVER (PARTITION BY SomethingElse) AS SomethingElseCount FROM ProductReviews WHERE ProductId = 1)s