Skip to content
Advertisement

Selecting Max Value, However Prioritising Certain Values

I have three tables that are joined. TableA has unique values for Column1 (ID) which joins on TableC on Column1 which has non-unique values. I’m currently joining these based off the max value for Column2 in TableC. The returning a value in TableB which is simply joined off TableC.

However I want to adjust this so that if TableB.Column2 has any value greater than 0 in TableC.Column2 then this is chosen as the max value, if it is 0 then the max value is chosen normally based off numeric value.

The current query I have is this:

What I am expecting to happen is that if:

TableC.Column2 > ‘0’ where TableB.Column2 = ‘KEYVALUE’ then show Table.Column2 based off TableC.Column3, however if TableC.Column2 = ‘0’ where TableB.Column2 = ‘KEYVALUE’ then show result of [TableB].Column2 based off MAX [TableC].Column2

Sample Data:

Sample Data

Example Output:

S7000,KEYVALUE

S6500,OTHERVALUE1

Hope that all makes sense, thank you.

Advertisement

Answer

I find your conditions hard to follow, but you seem to want apply:

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