0

I have a following data

enter image description here

I want to create the Index Level as shown in last column based on Region, Country, Store Type and Location column. So Region column will have its own serial number for each new region i.e. 'APAC is 1 and then EMEA will be 2' similarly its same for other columns(Country, Store Type, Location). I have tried to use partition to get the result but i am not able to get desired results.

2 Answers 2

3

Use dense_rank() window function.

select *,
       concat(
        dense_rank() over(order by Region), ',',
        dense_rank() over(partition by Region order by Country), ',',
        dense_rank() over(partition by Region, Country order by [Store Type]), ',',
        dense_rank() over(partition by Region, Country, [Store Type] order by Location)
       ) as [Index Level]
  from Tab
Sign up to request clarification or add additional context in comments.

Comments

0

You use simple CONCAT function (https://msdn.microsoft.com/en-us/library/hh231515.aspx) or + operator to concatenate all the values together and divide all the values with comma.

1 Comment

No i am not asking concatenating values. I want to create those serial numbers based on hierarchy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.