I want to sort a column not by asc or desc, but I want it to sort according to the sequence that I set. for example I have this data:
Varchar1 | Varchar2 | Value | Something
1401_1 | 1401_1_9 | 1 | Something1
1401_1 | 1401_1_9 | 0 | Something2
1401_1 | 1401_1_11 | 1 | Something1
1401_1 | 1401_1_11 | 1 | Something2
1401_1 | 1401_1_13 | 0 | Something1
1401_1 | 1401_1_13 | 1 | Something2
1401_1 | 1401_1_15 | 0 | Something1
1401_1 | 1401_1_15 | 1 | Something2
1401_1 | 1401_1_17 | 1 | Something1
1401_1 | 1401_1_17 | 0 | Something2
1401_1 | 1401_1_19 | 1 | Something1
1401_1 | 1401_1_19 | 0 | Something2
1401_1 | 1401_1_21 | 1 | Something1
1401_1 | 1401_1_21 | 1 | Something2
1401_1 | 1401_1_23 | 0 | Something1
1401_1 | 1401_1_23 | 1 | Something2
1401_1 | 1401_1_1 | 0 | Something1
1401_1 | 1401_1_1 | 1 | Something2
1401_1 | 1401_1_3 | 1 | Something1
1401_1 | 1401_1_3 | 0 | Something2
1401_1 | 1401_1_5 | 1 | Something1
1401_1 | 1401_1_5 | 0 | Something2
1401_1 | 1401_1_7 | 1 | Something1
1401_1 | 1401_1_7 | 1 | Something2
here is my code:
SELECT *
FROM (SELECT TOP 12
[varchar2] AS [T2],
SUM(CASE WHEN [Type] = 'something1' THEN value END) AS something1,
SUM(CASE WHEN [Type] = 'something2' THEN value END) AS [something2]
FROM tbl_table
GROUP by [varchar2]
ORDER by [varchar2] DESC)x
ORDER BY [T2] ASC
That code produces something like this : 1401_1_1, 1401_1_11, 1401_1_13, 1401_1_15, 1401_1_17, 1401_1_19, 1401_1_21, 1401_1_23, 1401_1_3, 1401_1_5, 1401_1_7, 1401_1_9 ==> it sorting in varchar
I want it to sort according to this sequence : 1401_1_9, 1401_1_11, 1401_1_13, 1401_1_15, 1401_1_17, 1401_1_19, 1401_1_21, 1401_1_23, 1401_1_1, 1401_1_3, 1401_1_5, 1401_1_7
Is it possible to do that?
You can use below code:
Order by PARSENAME(REPLACE(@varchar2,'_','.'),3),
PARSENAME(REPLACE(@varchar2,'_','.'),2),
CASE WHEN PARSENAME(REPLACE([varchar2],'_','.'),1)>=9 THEN 1 ELSE 2 END,
PARSENAME(REPLACE(@varchar2,'_','.'),1)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.