有没有办法按特定列中的值排序?

时间:2017-03-24 18:50:52

标签: sql-server-2008 sql-order-by

SQL Server 2008

(select case 
when datepart(month, enrollments.enddate) <datepart(month, getdate()) 
then 'Expired'
when datepart(month, enrollments.enddate)=datepart(month, getdate())
then 'Requal required by end of this month'
when datepart(month, enrollments.enddate)-1=datepart(m, getdate()) 
then 'Requal required by end of next month'
when datepart(m, enrollments.enddate) >datepart(m, getdate()) 
then 'Current'
Else null
End) as [Certification Status], Enrollments.CurrentGrade Grade, 
students.Status [Associate Status]

from venrollmentsfilter enrollments
left join students on enrollments.studentindex = students.studentindex  left join affiliations on students.affiliationindex = affiliations.affiliationindex  left join schools on students.schoolindex = schools.schoolindex  left join programs on students.programindex = programs.programindex  left join sections on enrollments.sectionindex = sections.sectionindex  left join courses on sections.courseindex = courses.courseindex  left join teachers on sections.teacherindex = teachers.teacherindex  left join lmsterms on sections.lmstermindex = lmsterms.lmstermindex  left join semesters on lmsterms.semesterindex = semesters.semesterindex  left join catalogs on courses.catalogindex = catalogs.catalogindex  left join departments on courses.departmentindex = departments.departmentindex  left join studentregionlist on students.studentregionlistindex = studentregionlist.studentregionlistindex  

WHERE  Enrollments.Status  = 'active'  AND  Sections.Name  like '%Medical Requalification - q1%'  and datepart(year, enrollments.enddate) = datepart(yyyy, getdate()) 
order by [Certification Status]

我想订购“认证状态”列中的值,如下所示:已过期,本月底需要重新计算,下月底需要重新计算,当前。

Table values

2 个答案:

答案 0 :(得分:1)

您在CASE

上使用ORDER BY
ORDER BY CASE WHEN [Certification Status] = 'Expired' 
                  THEN 1
              WHEN [Certification Status] = 'Requal required by the end of this month' 
                  THEN 2
              WHEN [Certification Status] = 'Requal required by the end of next month' 
                  THEN 3
              WHEN [Certification Status] = 'Current' 
                  THEN 4
         END

编辑您需要将查询作为子查询,因为您正在创建列别名。然后,您可以使用order by

在外部查询中使用别名
SELECT *
FROM (
       <YourQuery>
      ) as T
ORDER BY CASE WHEN [Certification Status] = 'Expired' 
                  THEN 1
              WHEN [Certification Status] = 'Requal required by the end of this month' 
                  THEN 2
              WHEN [Certification Status] = 'Requal required by the end of next month' 
                  THEN 3
              WHEN [Certification Status] = 'Current' 
                  THEN 4
         END

答案 1 :(得分:0)

在select查询中添加另一列具有数值的列(例如,将其称为“证书状态优先级”),并按该字段排序。我在这里包含一个高级图片(您可能必须在内部查询中包含基本查询并在外部添加订单)

(select case 
when datepart(month, enrollments.enddate) <datepart(month, getdate()) 
then 'Expired'
when datepart(month, enrollments.enddate)=datepart(month, getdate())
then 'Requal required by end of this month'
when datepart(month, enrollments.enddate)-1=datepart(m, getdate()) 
then 'Requal required by end of next month'
when datepart(m, enrollments.enddate) >datepart(m, getdate()) 
then 'Current'
Else null
End) as [Certification Status],

select case 
when datepart(month, enrollments.enddate) <datepart(month, getdate()) 
then 4
when datepart(month, enrollments.enddate)=datepart(month, getdate())
then 3
when datepart(month, enrollments.enddate)-1=datepart(m, getdate()) 
then 2
when datepart(m, enrollments.enddate) >datepart(m, getdate()) 
then 1
Else null
End) as [Certification Status Priority],

Enrollments.CurrentGrade Grade, 
students.Status [Associate Status]

from venrollmentsfilter enrollments
left join students on enrollments.studentindex = students.studentindex  
left join affiliations on students.affiliationindex = affiliations.affiliationindex  
left join schools on students.schoolindex = schools.schoolindex  
left join programs on students.programindex = programs.programindex  
left join sections on enrollments.sectionindex = sections.sectionindex  
left join courses on sections.courseindex = courses.courseindex  
left join teachers on sections.teacherindex = teachers.teacherindex  
left join lmsterms on sections.lmstermindex = lmsterms.lmstermindex  
left join semesters on lmsterms.semesterindex = semesters.semesterindex  
left join catalogs on courses.catalogindex = catalogs.catalogindex  
left join departments on courses.departmentindex = departments.departmentindex  
left join studentregionlist on students.studentregionlistindex = studentregionlist.studentregionlistindex  

WHERE  Enrollments.Status  = 'active'  AND  Sections.Name  like '%Medical Requalification - q1%'  and datepart(year, enrollments.enddate) = datepart(yyyy, getdate()) 
order by case 
when datepart(month, enrollments.enddate) <datepart(month, getdate()) 
then 4
when datepart(month, enrollments.enddate)=datepart(month, getdate())
then 3
when datepart(month, enrollments.enddate)-1=datepart(m, getdate()) 
then 2
when datepart(m, enrollments.enddate) >datepart(m, getdate()) 
then 1
Else null
End