SELECT Cursor using top1

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.

I tried to run via cursor but without success. By the cursor would pass the 3 lines, but would need to consider only one of greater value.

How do I resolve this? Some help? Am I on the right track?

    
asked by anonymous 25.10.2018 / 23:42

2 answers

0

In SQL Server I would try to use:

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

It will "group" by COD_PEDIDO and sort the values of each cod.

    
26.10.2018 / 00:26
0

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

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 working example here: link

    
26.10.2018 / 16:41