SQL 查询:在 ORDER BY 列表中遇到常量表达式

SQL Query: A constant expression was encountered in the ORDER BY list

我收到以下错误:

A constant expression was encountered in the ORDER BY list, position 1.

我正在尝试按列中的单元格值排序。查询是三个 union select 语句。代码如下:

Declare @RefDate date = '2019-09-04';

Select
'On Roll' as 'Student Group',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All'  from totals

where leaving is null and admission <= GETDATE() and enrolment in ('single registration','main - dual registration')

Union

Select
'New',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All'  from totals

where admission > @RefDate and enrolment in ('single registration','main - dual registration')

Union

Select
'Leavers',
sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
sum(case when year = '9' then 1 else 0 end) as 'Y9 No.', sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
sum(1) as 'All'  from totals

where leaving > @RefDate and enrolment in ('single registration','main - dual registration')

order by
case 'Student Group'
when 'On Roll' then 1
when 'New' then 2
when 'Leavers' then 3   
end

如果这些组是完全独立的,您可以只使用聚合。相反,您可以使用 apply 来定义组,然后聚合:

Select v.Student_Group,
       sum(case when year = '7' then 1 else 0 end) as 'Y7 No.', sum(case when year = '7' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y7 %',
       sum(case when year = '8' then 1 else 0 end) as 'Y8 No.', sum(case when year = '8' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y8 %',
      sum(case when year = '9' then 1 else 0 end) a s 'Y9 No.',
       sum(case when year = '9' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y9 %',
       sum(case when year = '10' then 1 else 0 end) as 'Y10 No.', sum(case when year = '10' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y10 %',
       sum(case when year = '11' then 1 else 0 end) as 'Y11 No.', sum(case when year = '11' then 1 else 0 end)/cast(sum(1) as decimal) as 'Y11 %',
       sum(1) as All
from totals t cross apply
     (values (1, 'On Roll', case when leaving is null and admission <= GETDATE() then 1 end),
             (2, 'New', case when admission > @RefDate then 1 end),
             (3, 'Leaving', case when leaving > @RefDate then 1 end)
     ) v(ord, Student_Group, flag)
where enrolment in ('single registration', 'main - dual registration') and
      v.flag = 1
group by v.which, v.ord
order by v.ord;

希望对您有所帮助。

With t1 as (
    Select leaving, admission, enrolment,
           (case when year = '7' then 1 else 0 end) as Y7,
           (case when year = '8' then 1 else 0 end) as Y8,
           (case when year = '9' then 1 else 0 end) as Y9,
           (case when year = '10' then 1 else 0 end) as Y10,
           (case when year = '11' then 1 else 0 end) as Y11
    From totals),

 t2 as (
    Select 1 as [Student Group], Y7, Y8, Y9, Y10, Y11
    From t1
    Where leaving is null and admission <= GETDATE() and enrolment in ('single registration','main - dual registration')

    Union all

    Select 2, Y7, Y8, Y9, Y10, Y11
    From t1
    Where admission > @RefDate and enrolment in ('single registration','main - dual registration')

    Union all

    Select 3, Y7, Y8, Y9, Y10, Y11
    From t1
    Where leaving > @RefDate and enrolment in ('single registration','main - dual registration'))

/* Select result */
Select case when [Student Group] = 1 
            then 'On Roll'
            else case when [Student Group] = 2
                      then 'New'
                      else 'Leaving'
                 end
       end as [Student Group Name],
       sum(Y7) as [Y7 No.], 1.0 * sum(Y7) / count(1) as [Y7 %],
       sum(Y8) as [Y8 No.], 1.0 * sum(Y8) / count(1) as [Y8 %],
       sum(Y9) as [Y9 No.], 1.0 * sum(Y9) / count(1) as [Y9 %],
       sum(Y10) as [Y10 No.], 1.0 * sum(Y10) / count(1) as [Y10 %],
       sum(Y11) as [Y11 No.], 1.0 * sum(Y11) / count(1) as [Y11 %]
From t2
Group by [Student Group]
--Order by [Student Group]

/* Select result */
Select StudentGroup.[Student Group Name],
       t3.[Y7 No.], t3.[Y7 %],
       t3.[Y8 No.], t3.[Y8 %],
       t3.[Y9 No.], t3.[Y9 %],
       t3.[Y10 No.], t3.[Y10 %],
       t3.[Y11 No.], t3.[Y11 %]
From (
    Select [Student Group],
           sum(Y7) as [Y7 No.], 1.0 * sum(Y7) / count(1) as [Y7 %],
           sum(Y8) as [Y8 No.], 1.0 * sum(Y8) / count(1) as [Y8 %],
           sum(Y9) as [Y9 No.], 1.0 * sum(Y9) / count(1) as [Y9 %],
           sum(Y10) as [Y10 No.], 1.0 * sum(Y10) / count(1) as [Y10 %],
           sum(Y11) as [Y11 No.], 1.0 * sum(Y11) / count(1) as [Y11 %]
    From t2
    Group by [Student Group]
) t3

Inner join (
    Select 1 as [Student Group], 'On Roll' as [Student Group Name]
    Union
    Select 2, 'New'
    Union
    Select 3, 'Leaving'
) StudentGroup
    On t3.[Student Group] = StudentGroup.[Student Group]

Order by t3.[Student Group]

干杯!

已通过使用 UNION ALL 而不是 UNION 解决。