【问题标题】:SQL Server order by casesSQL Server 按案例排序
【发布时间】:2017-08-08 18:26:50
【问题描述】:

我使用的是 SQL Server 2012。

这是我的查询,我希望按此顺序而不是按字母顺序排序。

这是我想要的顺序:

Foreign,
Out of State,
Texas,
Unknown,
Total Enrollment

我使用了按案例排序,但出现错误:

'5'附近的语法不正确

我该如何解决?

查询:

declare @Term int = 20172;

select 
    [Category], [Last_Term], [Current_Term], [#Change], [%Change] 
from 
    (select 
         [Category],
         cast(round(((cast(y.Last_Term as float)) * 1), 2) as varchar(10)) as 'Last_Term',
         cast(round(((cast(y.Current_Term as float)) * 1), 2) as varchar(10))   as 'Current_Term',
         cast(round(((cast(y.Current_Term as float) -
                      cast(y.Last_Term as float)) * 1), 2) as varchar(10)) as '#Change', 
         cast(round((((cast(y.Current_Term as float) - 
                       cast(y.Last_Term as float)) / 
                      (cast(y.Last_Term as float))) * 100), 2) as varchar(10)) + '%' as '%Change'
     from
         (select
              Category, 
              Case
                 when year = substring(CAST (@Term- 10  as Varchar(10)),1,4) 
                    then 'Current_Term'
                 when year = substring(CAST (@Term - 20 as Varchar(10)),1,4) 
                    then 'Last_Term'
              End As ACAD_YEAR, 
              a.enroll 
          from
              (select 
                   case 
                      when dh.HOME = 'F' 
                         then 'Foreign' 
                      when dh.HOME = 'O' 
                         then 'Out of State' 
                      when dh.Home = 'I' 
                         then 'texas'
                      when dh.Home = 'U' 
                         then 'UnKnown'
                   end as Category, 
                   (CONVERT(INT, SUBSTRING(ACADEMIC_PERIOD, 1, 4))) - 1 as year, 
                   count(*) as enroll
               from
                   [IR_DW].[dbo].[dw_enrollment_F] d
               inner join 
                   dim_Time t on d.TIME_KEY = t.TIME_KEY
               inner join 
                   dim_Home dh on d.HOME_KEY = dh.HOME_KEY
               inner join 
                   dim_Student_Level sl on d.STUDENT_LEVEL_KEY = sl.STUDENT_LEVEL_KEY
               where 
                   ACADEMIC_PERIOD_ALL = @Term - 10 
                   or ACADEMIC_PERIOD_ALL = @Term
               group by 
                   dh.HOME, (CONVERT(INT,SUBSTRING(ACADEMIC_PERIOD,1,4))) - 1) a
           ) src 
       PIVOT 
           (SUM(Enroll) FOR ACAD_YEAR in ([Current_Term] , [Last_Term])) y
    ) a
order by
    case 
       when a.[Category] = 'Foreign' then 1 
       when a.[Category] = 'Out of State' then 2 
       when a.[Category] = 'Texas' then 3 
       when a.[Category] = 'Unknown' then 4

【问题讨论】:

  • 我认为您至少错过了查询的最后一行
  • 缺少END关键字 --> CASE WHEN ........ 然后 4 END

标签: sql sql-server stored-procedures sql-server-2012


【解决方案1】:

虽然您也缺少5 的情况,但您缺少END...

when a.[Category] = 'Total Enrollment' then 5

ORDER BY
 case when a.[Category] = 'Foreign' then 1 
      when a.[Category] = 'Out of State' then 2 
      when a.[Category] = 'Texas' then 3 
      when a.[Category] = 'Unknown' then 4
      when a.[Category] = 'Total Enrollment' then 5 --added this
      else 6 --catch all to make anything that fell outside of these categories unsorted afterwards
 end --added here

【讨论】:

  • 根据 OPs 数据,最好指出 ELSE 也丢失了。因此,如果 a.Category 不符合这些条件之一,它们在 case 表达式中将为空,因此在结果集中排名第一
  • 公平点肯定@Matt。我假设这些是该列中的 only 值。我会为此添加一行
猜你喜欢
  • 1970-01-01
  • 2011-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多