Give TRUE value to the first record

-1

I want to sort a table by 2 fields:

Producto-Precio
1-100
2-25
2-27
1-23
3-45

I make a select order by by Product and price but now what I want is to add a field to larger ones and let the table look like this: Product-Price-Result

1-23-TRUE
1-100-FALSE
2-25-TRUE
2-27-FALSE
3-45-TRUE

Can you help me define what the process would be like? Thanks!

    
asked by Anna_MS 18.04.2017 в 18:40
source

2 answers

2

In the case of SQL Server, the other way, without sub-selects, could be:

select
  a.producto,
  a.precio,
  case row_number() over (partition by a.producto order by a.precio)
      when 1 then 'true'
      else 'false'
  end
  from MiTabla a
  order by a.producto,
           a.precio
    
answered by 19.04.2017 / 01:13
source
2

If I understood correctly, I think you're looking for something like this:

select
  a.producto,
  a.precio,
  resultado = case when a.precio = (select top 1 precio from MiTabla where producto=a.producto order by precio asc) then 'true' else 'false' end
from MiTabla a
order by  a.producto, a.precio asc;

you can try it here: link

    
answered by 18.04.2017 в 20:26