Skip to content
Advertisement

Max analytical function plus windowed ordering is not working as expected

I have a table with status and location, the following is the data. I’d like to get max status partitioned by location using custom ordering. Any idea what needs to change? Right now it is giving only max value, irrespective of the ordering i mentioned.

The custom ordering is 1 > 3 > 2

Expected result for location 11 : 1 Expected result for location 12 : 3

Query:

http://sqlfiddle.com/#!4/ed9e7e/13

Advertisement

Answer

I think you want first_value():

I’m not a big fan of decode(), but it is a concise way to express what you want. I prefer case expressions.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement