Call to Case When (syntax)

1

I have the following query:

  select ic14.cantidad_recepcionada as recepcionados
  from comex_015 ic15
  inner join comex_014 ic14 on ic15.invoice=ic14.invoice
  where ic14.codigo_producto='KO555581012EA40' and ic14.invoice !='0' 
  group by ic14.pedido
  order by ic14.pedido desc 
  limit 1

Which shows me the data ic14.cantidad_recepcionada , given the conditions mentioned,

until there is everything right ..

But I need to add a new condition, which shows me the data ic14.cantidad_recepcionada according to a record in the table comex_015

I need that when your registration in ic15.fecha_recepcion is null , do not show me the data you are currently bringing or show it at zero.

I was trying this way, but the result it shows is a INT DE VALOR 1 , when it should show a decimal of (11,0) which is < strong> ic14.cantidad_recepcionada which corresponds to 90 .

 select ic14.cantidad_recepcionada   = 
 (case when ic15.fecha_recepcion is not null then ic14.cantidad_recepcionada
  when ic15.fecha_recepcion is null then 0
  end) as recepcionado
 from comex_015 ic15
 inner join comex_014 ic14 on ic15.invoice=ic14.invoice
 where ic14.codigo_producto='KO555581012EA40' and ic14.invoice !='0' 
 group by ic14.pedido
 order by ic14.pedido desc 
 limit 1

    
asked by Francisco Acevedo 20.08.2018 в 17:20
source

1 answer

1

The problem is that what is evaluating your query is the result of the logical condition ic14.cantidad_recepcionada = CASE WHEN … , which can result in true or false ( 1 or 0 ).

What you really need is simply the result of the expression CASE :

 select  
 (case when ic15.fecha_recepcion is not null then ic14.cantidad_recepcionada
  when ic15.fecha_recepcion is null then 0
  end) as recepcionado
 from comex_015 ic15
 inner join comex_014 ic14 on ic15.invoice=ic14.invoice
 where ic14.codigo_producto='KO555581012EA40' and ic14.invoice !='0' 
 group by ic14.pedido
 order by ic14.pedido desc 
 limit 1
    
answered by 20.08.2018 / 17:59
source