How can I find out which one of the variables is the greatest

  • Background: a questionnaire. we count he number of 'A' or 'B' or 'C' or 'D' selected by a user in oder to display an answer depending on his choice.

    I'm in a stored procedure and I've got a value for each @a int; @b-2 int ; @C int ; @d int depending on the previosu long questinnaire filled by the user.

    I've no interest in the actual value of each variable , I only want to know which one is THE one with the greatest value.

    That seems quite a straight forward job...

    In despair of a clever function I was going to find out the median value, then exclude the lowest values and I would be left with the greatest vars to compare in a if .. elseif etc..

    Still that seems to me like a stupid / not clever boy solution

    I trust you guys to come up with a 4 lines solution (including the comment line 😉

    Thanks in advance

  • I know too many lines

    I did not account for a tie

    declare @a int

    declare @b-2 int

    declare @C int

    declare @d int

    set @a = 5

    set @b-2 = 1

    set @C = 4

    set @d = 2

    select top 1 letter

    from (select amt = @a, letter = 'a'

    union all

    select @b-2, 'b'

    union all

    select @C, 'c'

    union all

    select @d, 'd') as lettersAndScores

    order by amt desc

  • Indeed.

    A clever way of using "union all" to create a temp table and then querying the results.

    Thank you.

    Remi

  • It's not a clever way.

    It's just following one of normalization rules.

    Read it on your spare time.

    _____________
    Code for TallyGenerator

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply