Получение этой ошибки: не удалось связать составной идентификатор "c .hrmort" - PullRequest
0 голосов
/ 06 августа 2020

Все решения указывают на то, что если я назначил псевдонимы в своем коде, я должен использовать их вместо самого имени таблицы. Я назначил псевдоним, и я пытаюсь использовать псевдоним, однако я все еще получаю эту ошибку.

Это связано с использованием левого соединения?

Мой код выглядит следующим образом :

Select 
    x.MrtCategory
    
        from 
            (select case 
            when c.hrmort='CMHC' then 'CMHC'
            when c.hrmort='Genworth' then 'Genworth'
            when c.hrmort=''
                and a.purp in ('P16','P17') 
                and c.tenure in ('Freehold','Condo','Strata')
                and a.secval<1000000
                and a.amorty<=25
                and a.class in ('Standard','Stf Benefit Rate','Stf Member Rate')
                and a.totltov<80
            then 'Conventional Insurable'
            when c.hrmort IS NULL then 'Other'
            else 'Conventional UnInsurable'
            end as MrtCategory,
    sum(a.amount) as 'Amount'

from 
    ODS_WB.dbo.lnap as a left join ODS_WB.dbo.cust as b on a.no_=b.no_ and a.surname=b.surname
    left join ODS_WB.dbo.scur as c on b.rowno=c.rowno_custscur_cust and a.secval=c.secvalue and c.status='active'

where 
    year(a.appdate)=2020 and month(a.appdate)=6 and a.apptype='Mortgage' and a.sourcecode in ('FI',' ')) as x

group by 
    c.hrmort

1 Ответ

2 голосов
/ 06 августа 2020

Ваша группа по не входит в подзапрос, поэтому c.hrmort не существует. Вместо этого сгруппируйте по x.MrtCategory и просуммируйте сумму вне подзапроса:

Select x.MrtCategory,
       sum(x.amount) as Amount
from (
    select
        case 
          when c.hrmort='CMHC' then 'CMHC'
          when c.hrmort='Genworth' then 'Genworth'
          when c.hrmort=''
            and a.purp in ('P16','P17') 
            and c.tenure in ('Freehold','Condo','Strata')
            and a.secval<1000000
            and a.amorty<=25
            and a.class in ('Standard','Stf Benefit Rate','Stf Member Rate')
            and a.totltov<80
            then 'Conventional Insurable'
          when c.hrmort IS NULL then 'Other'
          else 'Conventional UnInsurable'
        end as MrtCategory,
--        sum(a.amount) as 'Amount'
        a.amount
    from      ODS_WB.dbo.lnap as a
    left join ODS_WB.dbo.cust as b on a.no_=b.no_ and a.surname=b.surname
    left join ODS_WB.dbo.scur as c on b.rowno=c.rowno_custscur_cust and a.secval=c.secvalue and c.status='active'
    where year(a.appdate)=2020
    and   month(a.appdate)=6
    and   a.apptype='Mortgage'
    and   a.sourcecode in ('FI',' ')
) as x
--group by c.hrmort -- c doesn't exist outside of the above subquery
group by x.MrtCategory
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...