Doubt in SQL query with Group by and Order by

Asked

Viewed 223 times

0

Good afternoon Galera!!!

I have an sql query that is simple, but I’m having a hard time doing an order by in two fields at once. Such consultation is for me to assemble a report, but before mounting I need to validate this query. The query being performed is this:

select 
   cped.razsocial,
   cped.nrpedido,
   iped.codbarra,
   iped.codprod,
   iped.qtde,
   iped.descricao,
   funcionario.fantasia,
   cped.dtpedido,
   produto.observacao,
   cped.liberado,
   cped.statusweb
from cped

inner join iped on (cped.nrpedido = iped.nrpedido)
inner join produto on (iped.codprod = produto.codprod)
inner join funcionario on (cped.indicacao = funcionario.funcionario)
inner join almox on (cped.almoxarifado = almox.codigo )

where (
  (cped.tipo = 'Z') )


group by 
  cped.razsocial,
  cped.nrpedido,
  iped.codbarra,
  iped.codprod,
  iped.qtde,
  iped.descricao,
  funcionario.fantasia,
  cped.dtpedido,
  produto.observacao,
  cped.liberado,
  cped.statusweb

order by iped.descricao,cped.nrpedido

When executing the command works, however it only orders by iped.descricao. I need it to be sorted by description but the cped.nrpedido field is attached, that is, if the customer has 3 items within the order with descriptions of different items, that he does not throw the request at the end of the list, but leave grouped.

Thanks in advance.

1 answer

0

TL;DR;

If I correctly inferred your model, to get the desired result your order by it must be so:

order by cped.razsocial, cped.nrpedido, iped.descricao

Explaining

You don’t order by two fields at the same time. You order one field and then another.

I think you have confused the concept of ordering a little bit. Look, assuming the following data set:

joão  | banana  | 1
joão  | maçã    | 1
maria | banana  | 2
maria | banana  | 3
maria | maça    | 3
maria | banana  | 4
joão  | maçã    | 5
joão  | acerola | 5
maria | acerola | 6

According to your consultation, the result of the sorting for this data set would be thus:

joão  | acerola | 5
maria | acerola | 6
joão  | banana  | 1
maria | banana  | 2
maria | banana  | 3
maria | banana  | 4
joão  | maçã    | 1
maria | maça    | 3
joão  | maçã    | 5

That is, it is ordered by description of items and next for order number. First they’re all gonna show up with their orders and then they’re gonna show up with the bananas and then they’re gonna list the apples. Regardless of what order these items were made.

By his text, his expectation would be that result:

joão  | banana  | 1
joão  | maçã    | 1
joão  | maçã    | 5
joão  | acerola | 5
maria | banana  | 2
maria | banana  | 3
maria | maça    | 3
maria | banana  | 4
maria | acerola | 6

In other words: Ordained by client, next for request and next for item description.

I hope I’ve helped.

  • Good morning Diego, thank you for the answer! The first field I need to start ordering would be the product description. I understood your explanation, but as I have the request to start ordering by this field, I need to order the list of products without losing the relation of the order, ie ordering by description and keeping the products together to the same order.

  • @Jhonnycosta this would be a contradiction. The two things are mutually exclusive. Either you understand the explanation (and give up what you thought) or you keep insisting on the other line of thought (and you don’t understand).

  • In the last example I gave, by what you’re saying you’d be wanting it to appear acerola of the request 5 of joão, then the maçã of the request 5 of joão and then the acerola of the request 6 of maria... If this is the case, your resultset is not ordered by the description of the item, as if they were all acerolas should appear before the maçãs (which would require breaking orders)

  • This even explains the fact that you have included a group by for all columns and not being using any stapling function or still doing a join released with the almox without using it for any purpose. Either this is another requirement not spelled out in the question (in which case it would have entered the [mcve] by mistake) or actually its requirements / query do not even make sense

Browser other questions tagged

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