Select conditional that takes value from another table

Asked

Viewed 93 times

0

I need to make a conditional select that checks a sum performed that way:

select round(sum((`gin`.`Caixa_9L` / 1000)),1) AS `total` from `gin`

If this sum above is = 0, I need to take a value of a field in another table (called asterisk) and display as total.

I’m starting in sql and I’m having a little trouble defining this logic.

1 answer

2


select case CAIXA_9L when ( round(sum((Caixa_9L / 1000)),1)  = 0 ) then
     ( select outrocampo from asterico ) /* não pode retornar mais de um registro esse subselect */ 
else
     ( round(sum((Caixa_9L / 1000)),1)  = 0 )
end as total from gin
  • You referenced in from only the gin table, the asterisk table does not need? When I spin here, it tells me that gin.asterico does not exist.

  • Friend, I tested the code with some adaptations and this occurring the following: When the Box field_9l has a value registered as zero, the query does not add * but 0.0, when the Box field_9l contains at least 1 (a unit), it puts *. What can be this?

  • select 
marca,
case 
 gin.Caixa_9L when (round(sum(caixa_9l/1000),1)= 0) then (select asterisco.asterisco from asterisco)
else
 (round(sum((Caixa_9L/1000)),1))
end
as valor
from gin
where sales_channel = 'duty paid' or sales_channel = 'travel retail'
group by marca My query looked like this.

Browser other questions tagged

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