I have this:
select CODART as 'MODELO / REF', DESCLIN as DESCRIPCION, UNIDADES as CANTIDAD
from LINEPEDI
where IDPEDV in ('69782','69780','68931','69781')
In the result I would like it to return me in the order I have put it but by default it returns it sorted by id.
Would anyone know how to tell him not to order it by id?
CodePudding user response:
To sort rows in the result set you need to use ORDER BY
. Otherwise the engine is free to return them with any ordering.
You can do:
select
CODART as "MODELO / REF",
DESCLIN as DESCRIPCION,
UNIDADES as CANTIDAD
from LINEPEDI
where IDPEDV in ('69782','69780','68931','69781')
order by case IDPEDV when '69782' then 1
when '69780' then 2
when '68931' then 3
when '69781' then 4
end
CodePudding user response:
It might be easier to use something like a table variable here, with an IDENTITY
:
DECLARE @IDPEDV AS table (ID int IDENTITY(1, 1),
IDPEDV varchar(5));
INSERT INTO @IDPEDV (IDPEDV)
VALUES ('69782'),
('69780'),
('68931'),
('69781');
SELECT CODART AS [MODELO / REF],
DESCLIN AS DESCRIPCION,
UNIDADES AS CANTIDAD
FROM dbo.LINEPEDI L
JOIN @IDPEDV ON L.IDPEDV = I.IDPEDV
ORDER BY I.ID;