Click here to Skip to main content
15,891,905 members
Please Sign up or sign in to vote.
1.14/5 (4 votes)
See more:
suppose i wrote query :select employees from employee where employeeid in(2,3,5,1)
then i want to fetch data according to 2,3,5,1 order. but it display 1 ,2,3,5 wise please help me?
"select m.MachineID,jobcode,Fault,Running,FaultLimitExceed,ProdCount from machinemaster m inner join machinestatuspara s " +" on m.machineid=s.machineid where m.machineid is not null and m.machineid in(2,1)";

ans:
'1', '1', '0', '0', '0', '2377', '00:57:42', '31-10-2014'
'2', '3', '0', '0', '0', '1492', '00:57:35', '31-10-2014'
but i want


'2', '3', '0', '0', '0', '1492', '00:57:35', '31-10-2014'
'1', '1', '0', '0', '0', '2377', '00:57:42', '31-10-2014'
Posted
Updated 17-Nov-14 19:01pm
v5
Comments
Sergey Alexandrovich Kryukov 17-Nov-14 23:45pm    
Not clear. Record what? in what "format"? Stored procedure? Anything else? What are you talking about?
—SA
syed shanu 18-Nov-14 0:28am    
can you paste your full query here so that it will be easy to find your problem,need to check wheater are you using any order by in you query.
Sinisa Hajnal 18-Nov-14 2:15am    
If I understand correctly, he would like his list ordered by the order of his condition, that is list should be ordered by 2,3,5,1 just as he entered IN condition.

Unfortunately, IN works as series of ORs and there is no guarantee of any order unless ORDER BY is specified.

In this particular case ORDER BY time field or 1492 would work, but this is too small sample to know for sure. Post more details.
RKMGROUP 18-Nov-14 4:25am    
Hi,

Please use below example, it's helpful for you.

create table #temp
(
empid int
)

insert into #temp(empid) values(1)
insert into #temp(empid) values(2)
insert into #temp(empid) values(3)
insert into #temp(empid) values(5)


select * from #temp order by empid%2

-----------RAMESH-------------

XML
Please use below example, it's helpful for you.
<pre lang="sql">create table #temp
(
empid int
)

insert into #temp(empid) values(1)
insert into #temp(empid) values(2)
insert into #temp(empid) values(3)
insert into #temp(empid) values(5)


select * from #temp order by empid%2</pre>
 
Share this answer
 
Hello ,
As there is a primary Key on the table. so,you will always get the order by Ascending clause.Because of clustered Indexes.Refer the link For More Explanation:Code Project:Indexes
MSDN

There is one possible solution. you need to make a stored procedure for this.In which First Insert a Record in the temp table.Created a PRimary Key on that temp table.Then Insert the record in sequence in which you want one by one.

Hope It Might Helps You.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900