SQL - DESC ordering in ROW_NUMBER

Dear Experts,

Could you please have a look on the attached small query, basically I would like just assign a number 1 by ROW_NUMBER to that region settings, which is set most often in SAP.

SELECT
	ROW_NUMBER() OVER (PARTITION BY #RegionsCheck.CountryCity ORDER BY COUNT(#RegionsCheck.CountRegions) DESC) AS MostFrequentSettings,
	#RegionsCheck.Country,
	#RegionsCheck.City,
	#RegionsCheck.CountryCity,
	#RegionsCheck.Region,
	#RegionsCheck.CountRegions
INTO
	#RegionsCheckMostFrequent
FROM
	#RegionsCheck
GROUP BY
	#RegionsCheck.Country,
	#RegionsCheck.City,
	#RegionsCheck.CountryCity,
	#RegionsCheck.Region,
	#RegionsCheck.CountRegions

-- SELECT * FROM #RegionsCheckMostFrequent ORDER BY CountryCity ASC, CountRegions DESC

Open in new window


In the example concretely - the city 'Hamme' in Belgium has 7 times set '08', while 2 times '02', so would expect that in the MostFrequentSettings column the 1 should be beside of the '08' line.

Printscreen about the result of the query
But as you can see orange marked somehow that works opposite, could you please advise why and how it could be corrected?

Thanks in advance,
LVL 1
csehzIT consultantAsked:
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.

Mark WillsTopic AdvisorCommented:
Well, count in that setting doesnt work - it ends up a progressive count

you are better off select from a derived table (or CTE) to get your counts worked out first...

SELECT
	ROW_NUMBER() OVER (PARTITION BY a.CountryCity ORDER BY CountedRegions DESC) AS MostFrequentSettings,
	a.Country,
	a.City,
	a.CountryCity,
	a.Region,
	a.CountRegions
INTO
	#RegionsCheckMostFrequent
FROM

(SELECT COUNT(*) as CountedRegions,
	#RegionsCheck.Country,
	#RegionsCheck.City,
	#RegionsCheck.CountryCity,
	#RegionsCheck.Region,
	#RegionsCheck.CountRegions
FROM
	#RegionsCheck
GROUP BY
	#RegionsCheck.Country,
	#RegionsCheck.City,
	#RegionsCheck.CountryCity,
	#RegionsCheck.Region,
	#RegionsCheck.CountRegions) a

Open in new window

or there abouts...
0

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
csehzIT consultantAuthor Commented:
Thanks for the answer
0
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
SQL

From novice to tech pro — start learning today.