简体   繁体   English

使用分组依据时出现无效的列名错误

[英]Invalid column name error while using group by

I am getting Invalid column error on acctkey column. 我在acctkey列上收到无效列错误。 Can you please tell me what am I doing wrong? 你能告诉我我在做什么错吗?

select COUNT(acctkey) as Num, case
            when source_sys_cd in ('TRS','CLS') then source_sys_cd + '|'+[acct_num]                 
            when source_sys_cd = 'Hogan CIS' and [acct_type_cd] = 'DDA' then 'DDA' + '|'+ [acct_id]
            when source_sys_cd = 'Hogan CIS' and [acct_type_cd] != 'DDA' then 'TDA' +'|'+ [acct_id]
            when source_sys_cd = 'CLN' then source_sys_cd + '|'+ [acct_num]+ [acct_id]
            when source_sys_cd = 'RCC' then source_sys_cd + '|'+ [acct_id]
            when source_sys_cd = 'ITF' then source_sys_cd + '|'+ [acct_id]+ [acct_num]
            when source_sys_cd = 'SEC' then source_sys_cd + '|'+ [acct_id]
            else source_sys_cd + '|'+ [acct_num]
            end  as acctkey
            from mtb..STAGING_CUST_ACCT
            group by source_sys_cd
            ,acct_id
            ,acct_num
            ,acctkey
            ,acct_type_cd

In your case, you could technically fix the problem just by removing acctkey from the group by: 就您而言,您可以通过acctkey从技术上仅通过从组中删除acctkey来解决问题:

        group by source_sys_cd, acct_id, acct_num, acct_type_cd

All these columns are included in the definition of the acctkey (if I didn't miss anything). 所有这些列都包含在acctkey的定义中(如果我什么都没错过的话)。 However, I doubt that is what you really want, because that is likely to produce duplicates for the acctkey . 但是,我怀疑那是您真正想要的,因为这很可能会为acctkey产生重复acctkey

Instead, I think you want: 相反,我认为您想要:

with t as (
        select t.*,
           (case
            when source_sys_cd in ('TRS','CLS') then source_sys_cd + '|'+[acct_num]                 
            when source_sys_cd = 'Hogan CIS' and [acct_type_cd] = 'DDA' then 'DDA' + '|'+ [acct_id]
            when source_sys_cd = 'Hogan CIS' and [acct_type_cd] != 'DDA' then 'TDA' +'|'+ [acct_id]
            when source_sys_cd = 'CLN' then source_sys_cd + '|'+ [acct_num]+ [acct_id]
            when source_sys_cd = 'RCC' then source_sys_cd + '|'+ [acct_id]
            when source_sys_cd = 'ITF' then source_sys_cd + '|'+ [acct_id]+ [acct_num]
            when source_sys_cd = 'SEC' then source_sys_cd + '|'+ [acct_id]
            else source_sys_cd + '|'+ [acct_num]
            end)  as acctkey
        from mtb..STAGING_CUST_ACCT t
       )
select count(*), acctkey
from t
group by acctkey;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM