SQL Запрос: в списке ORDER BY обнаружено постоянное выражение - PullRequest
0 голосов
/ 13 января 2020

Я получаю следующую ошибку:

В списке ORDER BY, позиция 1, обнаружено постоянное выражение.

Я пытаюсь упорядочить по значения ячеек в столбце. Запрос состоит из трех операторов выбора объединения. Код ниже:

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

Ответы [ 3 ]

0 голосов
/ 13 января 2020

Надеюсь, это поможет.

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]

Приветствия!

0 голосов
/ 13 января 2020

Решено с помощью UNION ALL, а не UNION.

0 голосов
/ 13 января 2020

Если бы группы были полностью отдельными, вы могли бы просто использовать агрегацию. Вместо этого вы можете использовать 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;
...