2

I am trying to order rows by string column like this:

select * from cards
order by (case gate 
          when 'mastercard' then 1
          when 'visa' then 2
          when 'fibi' then 3
          when 'uzcard' then 4
          when 'humo' then 5 
          end)

But I want mastercard and visa have the same index. I tried something like this:

select * from cards
order by (case gate 
          when 'mastercard' or 'visa' then 1
          when 'fibi' then 2
          when 'uzcard' then 3
          when 'humo' then 4
          end)

but I get this error:

ERROR: invalid input syntax for type boolean
4
  • 1
    "didn't work" is not a problem description. What did it do? Why was that wrong? Sample input and desired output would greatly help, and current output and an explanation of what's wrong with it. Commented Jun 1, 2021 at 9:17
  • 3
    Simply use when 'mastercard' then 1 and when 'visa' then 1 . Commented Jun 1, 2021 at 9:19
  • Case expression, not statement... Commented Jun 1, 2021 at 9:26
  • 1
    @jarlh my English is not that good, sorry. I will edit Commented Jun 1, 2021 at 9:30

2 Answers 2

3

Either create a second case with the same result, or convert your case to a full conditional.

CASE has two forms: the base form is

CASE 
    WHEN condition THEN result
    WHEN condition THEN result
    ...
END

in which case condition is an arbitrary boolean expression, similar to a sequence of if/else if/else if in C, or the shortcut

CASE expression
    WHEN value THEN result
    WHEN value THEN result
    ...
END

in which case value is... just a simple value, similar to a switch statement in C.

Here you have the second form. 'mastercard' or 'visa' is just plain nonsense, it's not SQL at all as it will attempt to treat 'mastercard' and visa' as booleans (which will fail, because neither is a valid boolean literal), then OR them, then will check if gate is equal to the result.

You can either add a new case with the same result

case gate 
          when 'mastercard' then 1
          when 'visa' then 1
          when 'fibi' then 2
          when 'uzcard' then 3
          when 'humo' then 4
          end

or expand the statement to a full conditional:

case
    when gate = 'mastercard' or gate = 'visa' then 1
    when gate = 'fibi' then 2
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

@underscore_d can you answer the question with in example please?
@DilsMatchanov thinking about it properly now, it wouldn't really help that much here, as Masklinn said, as it would quite a bit longer / more repetitive. But what I meant was you would write e.g. case when gate in ('mastercard', 'visa') then 1 when gate = 'fibi' then 2... etc. The in operator allows comparing the lhs against a set of multiple values.
1

you can also use or operator here, this way you can prevent multiple when with same values and the list can be extended as well:

Case when gate in ('master_card','visa') then 1
     when gate = 'fibi' then 2
     .....
End as [column name as you want it to be]

Comments

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.