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.
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.
– Jhonny Costa
@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).
– Diego Rafael Souza
In the last example I gave, by what you’re saying you’d be wanting it to appear
acerola
of the request5
ofjoão
, then themaçã
of the request5
ofjoão
and then theacerola
of the request6
ofmaria
... If this is the case, your resultset is not ordered by the description of the item, as if they were allacerolas
should appear before themaçãs
(which would require breaking orders)– Diego Rafael Souza
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 ajoin
released with thealmox
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– Diego Rafael Souza