SELECT Cursor using top1

Asked

Viewed 59 times

0

I need to return only the highest value of one of the lines below, taking into account that I also need to submit the order code.

inserir a descrição da imagem aqui

I tried to rotate via cursor but without success. The cursor would pass the 3 lines, but would need to consider only one of higher value.

How do I fix it? Any help? I’m on the right track?

  • what criteria do you want to use for "cod_request", the highest or the lowest? Since the other fields are the same, you need to sort in some way. You must return which of the "cod_requested"?

  • There is no criterion, the important thing is to return the higher value of one of the two orders. We can consider it a last request, provided it has the highest value.

2 answers

0

In SQL Server I would try to use:

row_number()over(partition by COD_PEDIDO order by VALOR desc)as Ordem

He will "group" by COD_PEDIDO and sort the values of each Cod.

0

You could use a max(cod_pedido) or min(cod_pedido) to bring only one value.
It would be something like that:

SELECT dt_pedido,
       cod_cli,
       max(cod_pedido) as cod_pedido,
       max(valor) as valor
FROM exemplo
GROUP BY dt_pedido,
         cod_cli

You can see a functional example here: http://sqlfiddle.com/#! 4/c168c/1

Browser other questions tagged

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