Select Conditional with another Select

Asked

Viewed 42 times

0

I’m using SQL Oracle. I need to return a Select that depending on the value of the column GRUPO_ECONOMICO_ID (needs to be equal to 87) it must run a Select, otherwise it must run another select... I believe I’m close to what I need, but it triggers an error that a keyword has not been found... What I’m doing wrong??

select CASE GRUPO_ECONOMICO_ID
       WHEN ((select GRUPO_ECONOMICO_ID from empresa where codigo = :TOKEN) = 87)
           THEN
               (SELECT emp.id
                from empresa emp
                where emp.grupo_economico_id = 87
                union
                SELECT emp.id
                from empresa emp
                WHERE codigo = (vou chumbar um token aqui ainda a ser criado, mas ele e diferente dos outros TOKENs aqui descritos)
           ELSE
               (SELECT emp.id
                FROM empresa emp
                WHERE emp.grupo_economico_id = (
                    SELECT grupo_economico_id
                    FROM empresa
                    WHERE codigo = :TOKEN
                ))
           END as total from empresa;
  • where codigo = :TOKEN) = 87 this where shouldn’t be where GRUPO_ECONOMICO_ID = 87?

  • I don’t think because this guy does a search in the company table for the token that was passed by the user and there may return 87... Then the conditional comes from (select GRUPO_ECONOMICO_ID from company Where code = :TOKEN)

  • Confusing, at least for me , could give an example with data ?

  • Of course the idea is that when you research a company X, the user gets information from all the companies that belong to the same economic group as this company X, right? What happens is that there is a select rule specific for when specifically the economic group is 87, so I need some kind of conditional (IF.. ELSE also serves) within sql oracle to run a select or another depending on the economic group (specifically 87).

1 answer

0

In case I think that what is bothering you would indicate a field in front of CASE, oracle allows you to make a case without indicating this key.

select CASE 
       WHEN ((select GRUPO_ECONOMICO_ID from empresa where codigo = :TOKEN) = 87)
           THEN
               (SELECT emp.id
                from empresa emp
                where emp.grupo_economico_id = 87
                union
                SELECT emp.id
                from empresa emp
                WHERE codigo = (vou chumbar um token aqui ainda a ser criado, mas ele e diferente dos outros TOKENs aqui descritos)
           ELSE
               (SELECT emp.id
                FROM empresa emp
                WHERE emp.grupo_economico_id = (
                    SELECT grupo_economico_id
                    FROM empresa
                    WHERE codigo = :TOKEN
                ))
           END as total from empresa;

As indicated by the field in front of CASE, oracle expects it to be a simple comparison with this key, I could not simulate by laziness to create a similar database.

I hope it helps!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.