Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello dear,
Can u help me, please.
I have a Table in Ms Access contain data like this
ID Name
1 Dara
2 Somi
3 Vicheka
4 Sokun
5 Chann
6 Rith
7 Kaka
8 Nyla
9 sun
10 Lolana

I want to sort like this in Query or SQL Code, ...
3 Vicheka
2 Somi
1 Dara
6 Rith
5 Chann
4 Sokun
9 sun
8 Nyla
7 Kaka
10 Lolana

Can you help me do like below?
I waiting for result from all dear.
Posted

Yes you can:
SQL
SELECT 
    ID, [Name]
FROM 
    tblYourTable
ORDER BY 
    ([ID]-1) \ 3, ID DESC;

Note the backslash for integer division.
 
Share this answer
 
Comments
Maciej Los 30-Sep-15 7:18am    
5ed!
savin phat 1-Oct-15 2:24am    
Thank you sir
savin phat 1-Oct-15 2:30am    
mean the same question and sort like above, if I change ID to 4 digit
ID Name
1001 Dara
1002 Somi
1003 Vicheka
1004 Sokun
1005 Chann
1006 Rith
1007 Kaka
1008 Nyla
1009 sun
1010 Lolana
Help me please.
Thank q befor sir.
Gustav Brock 1-Oct-15 3:04am    
That would be:

SELECT
ID, [Name]
FROM
tblYourTable
ORDER BY
([ID]-2) \ 3, ID DESC;

Adjust the subtrahend between 0 and 2 to control the sequence.
As there is no sorting to do (the order you need cannot be expressed with comparison operators), you have to add a third column to your table, named for example OrderNumber, where you will put the index of the line in which you want it to appear.
Then, in your query, you just have to add a ORDER BY clause on the new column.
 
Share this answer
 
Comments
Krunal Rohit 30-Sep-15 1:05am    
Yeah.

-KR

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