MAX Function for an Over() results

I'm trying to simply take the max from a list created from an OVER()

From the query:
SELECT DISTINCT productCode,baseProductDescription, COUNT(*) OVER(PARTITION BY baseProductDescription) AS DescriptionCount
FROM dbo.BaseProducts
WHERE productcode NOT LIKE '[A-Z]%' 
ORDER BY productcode, baseProductDescription[code]

Open in new window

[/code]

Results from query
I get my results I want in the DescriptionCount column. So now i want to take the max from that column. I'm having a hard time getting this because i need the Order by and it won't let me create my CTE with it.  Simply using the Max(BaseProductDescrition) wont give me the results i want.


This may be simpler than i think.
Southern_GentlemanAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
see if this worked for you?

;with cte as
(
SELECT DISTINCT productCode,baseProductDescription, COUNT(*) OVER(PARTITION BY baseProductDescription) AS DescriptionCount
FROM dbo.BaseProducts
WHERE productcode NOT LIKE '[A-Z]%' 
)
select top 1 * from cte
ORDER BY DescriptionCount desc, productcode, baseProductDescription desc

Open in new window

Southern_GentlemanAuthor Commented:
This is the results that it brought back

Results
I'm looking to bring back

Productcode  BaseProductDescription  Count
0018  Tax Exempt 159
1000 JET A   255
10010  JET A 255
Etc etc.

It should be the max for each productcode
Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
then try:

;with cte as
(
SELECT DISTINCT productCode,baseProductDescription, COUNT(*) OVER(PARTITION BY baseProductDescription) AS DescriptionCount
FROM dbo.BaseProducts
WHERE productcode NOT LIKE '[A-Z]%' 
), cte2 as
(
      select *, Row_number() over (partition by productCode order by DescriptionCount desc ) idx from cte
)
Select productCode, baseProductDescription, DescriptionCount from cte2 where idx = 1

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Southern_GentlemanAuthor Commented:
thanks Ryan, I guess I just had to go one CTE deeper.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.