收到此错误:无法绑定多部分标识符“c.hrmort”

vawmfj5a  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(234)

所有的解决方案都指向这样一个事实:如果我在代码中指定了别名,我应该使用它们而不是表名本身。我已经分配了别名,我正在尝试使用别名,但是,我仍然收到此错误。
这与使用左连接有关吗?
我的代码如下:

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
von4xj4u

von4xj4u1#

您的分组依据在子查询之外,因此 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

相关问题