我有一个列名session
,其中会话的存储方式类似于
fall-2018
fall-2019
spring-2019
spring-2020
我想按这样的升序对结果进行排序:
Fall-2018
Spring-2019
Fall-2019
Spring-2020
我尝试过的查询:
SELECT *
FROM student
ORDER BY session +0 ASC
请建议!
注意:还有五列,我必须全选所有列,然后按会话排序
答案 0 :(得分:0)
编辑:确定,然后在MS SQL中
with student as
(
select 'fall-2018' as "session" union all
select 'spring-2018' union all
select 'fall-2019' union all
select 'spring-2019' union all
select 'fall-1980' union all
select 'spring-2055' union all
select 'fall-2052'
)
select
s."session"
from student s
order by right(s."session", 4), s."session"
好吧,在Oracle中,这个 strings 可以像这样排序
with student as
(
select 'fall-2018' as "session" from dual union all
select 'spring-2018' from dual union all
select 'fall-2019' from dual union all
select 'spring-2019' from dual
)
select
initcap(s."session")
from student s
order by substr(s."session", -4), s."session"
答案 1 :(得分:0)
您可以拆分年份部分,并以此排序
SELECT Session
FROM student
order by PARSENAME(Replace(Session,'-','.'), 1)
答案 2 :(得分:0)
另一种可能的方法是使用条件ORDER BY
:
CREATE TABLE #Students (
[Session] varchar(20)
)
INSERT INTO #Students ([Session])
VALUES
('fall-2018'),
('fall-2019'),
('spring-2019'),
('spring-2020')
SELECT [Session]
FROM #Students
ORDER BY
SUBSTRING([Session], CHARINDEX('-', [Session]) + 1, 4),
CASE
WHEN CHARINDEX('spring-', [Session]) > 0 THEN 1
WHEN CHARINDEX('fall-', [Session]) > 0 THEN 2
ELSE 3
END
输出:
Session
----------------
fall-2018
spring-2019
fall-2019
spring-2020
答案 3 :(得分:0)
我不确定我的答案会满足您的要求,因为您未指定升序,降序等排序类型。
但是,我已经按照您的预期结果完成了操作。
此代码供您参考。
Create table #Temp (Value varchar(100))
insert into #Temp values ('fall-2018')
insert into #Temp values ('fall-2019')
insert into #Temp values ('spring-2019')
insert into #Temp values ('spring-2020')
SELECT Value
FROM #Temp order by Convert(int,PARSENAME(Replace(Value,'-','.'), 1)) ,
SUBSTRING(Value, 1, CHARINDEX('-',Value)-1) desc
答案 4 :(得分:0)
如果最后四位数字始终是年份,那么这种方法也将帮助您获得所需的输出。
;with Cte as (Select 'Fall-2018' as [Session] union all
Select 'Fall-2019' as [Session] union all
Select 'Spring-2019' as [Session] union all
Select 'Spring-2020' as [Session] )
select * from cte
order by right(session,4), case when Session like '%Spring%' then 'a'+ session else session end ;
基于输出,我必须在有弹簧的情况下添加a,以便在下降之前进行排序。正确将有助于确保我们在同一年进行排序。
输出:
Session
Fall-2018
Spring-2019
Fall-2019
Spring-2020
如果它也适用于Fall Fall Spring Spring格式(基于年份,然后是字母顺序)。您可以使用
;with Cte as (Select 'Fall-2018' as [Session] union all
Select 'Fall-2019' as [Session] union all
Select 'Spring-2019' as [Session] union all
Select 'Spring-2020' as [Session] )
select * from cte
order by right(session,4), Session ;
输出:
Session
Fall-2018
Fall-2019
Spring-2019
Spring-2020
答案 5 :(得分:0)
您可以尝试以下基于year
并以字符-
分隔的查询。
请记住,当您添加列作为别名并在该列上提供order by
时,该列就可以在该列上正常工作而不会出现任何错误。这里的YearName
是运行时作为列别名。
with sSession([Session])
as
(
select 'fall-2018'
union all
select 'fall-2019'
union all
select 'spring-2019'
union all
select 'spring-2020'
)
select [Session] from(
select [Session],
SUBSTRING([Session], CASE CHARINDEX('-', [Session])
WHEN 0
THEN LEN([Session]) + 1
ELSE CHARINDEX('-', [Session]) + 1
END, 1000) AS YearName
from sSession
)a
order by YearName
输出如下所示
Session
----------
fall-2018
spring-2019
fall-2019
spring-2020
您可以在此处找到演示-Demo<>Order BY
答案 6 :(得分:0)
您可以先按年份(最后4个字符),然后按季节对它们进行排序,如下所示:
select session
from students
order by right(session, 4),
session desc;
session
(即季节)按降序排列,因为在任何一年中,春季都在秋季之前。