如何在内部查询中使用WITH

时间:2014-03-20 17:01:32

标签: sql sql-server common-table-expression with-statement

我在内部查询中使用了公用表表达式,但它在WITH附近抛出错误消息319。它要求我使用;在它之前,但当我使用;在WITH之前,它会抛出语法错误。有人可以帮忙吗?我正在使用sql server。

insert tb_acct_address
    (
        [acct_key]
        ,[acct_num]
        ,[addr_type_cd]
        ,[addr_line_1]
        ,[addr_line_2]
        ,[addr_line_3]
        ,[city]
        ,[state_province]
        ,[postal_code]
        ,[country_cd]
        ,[address_flag]
        ,[batch_dt]
    )
    select distinct
        [AcctKey]
        ,[AcctNum]
        ,[address_type_cd]
        ,[addr_1]
        ,[addr_2]
        ,[addr_3]
        ,[city]
        ,[state]
        ,[zip]
        ,[country_cd]
        ,[ad_flag]
        ,[batch_dt]
    from
    ( 

    with cte as 

    (

    select distinct
        case
                            WHEN APPLICATION = 'Hogan CIS' AND d.[ACCOUNT PRODUCT   SERVICE CODE] = 'DDA' THEN 'DDA' + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            WHEN APPLICATION = 'Hogan CIS' AND d.[ACCOUNT PRODUCT   SERVICE CODE] != 'DDA' THEN 'TDA' +'|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            WHEN APPLICATION = 'CLN' THEN APPLICATION + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR) + CAST(d.[ACCOUNT NUMBER] AS VARCHAR)
                            WHEN APPLICATION = 'ITF' THEN APPLICATION + '|'+ CAST(d.[CLIENT KEY] AS VARCHAR) + CAST(d.[CLIENT ID] AS VARCHAR)
                            WHEN APPLICATION IN ('RCC','SEC') THEN APPLICATION + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            ELSE APPLICATION + '|'+ CAST(d.[ACCOUNT NUMBER] AS VARCHAR) 
                            END  as [AcctKey]
        ,case 

                            WHEN APPLICATION = 'Hogan CIS' AND d.[ACCOUNT PRODUCT   SERVICE CODE] = 'DDA' THEN 'DDA' + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            WHEN APPLICATION = 'Hogan CIS' AND d.[ACCOUNT PRODUCT   SERVICE CODE] != 'DDA' THEN 'TDA' +'|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            WHEN APPLICATION = 'CLN' THEN APPLICATION + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR) + CAST(d.[ACCOUNT NUMBER] AS VARCHAR)
                            WHEN APPLICATION = 'ITF' THEN APPLICATION + '|'+ CAST(d.[CLIENT KEY] AS VARCHAR) + CAST(d.[CLIENT ID] AS VARCHAR)
                            WHEN APPLICATION IN ('RCC','SEC') THEN APPLICATION + '|'+ CAST(d.[ACCOUNT ID] AS VARCHAR)
                            ELSE APPLICATION + '|'+ CAST(d.[ACCOUNT NUMBER] AS VARCHAR) 
                            END as [AcctNum]
        ,case 
            when len(ltrim(rtrim(d.[primary city]))) > 1 then 'P'
            when len(ltrim(rtrim(d.[mailing city]))) > 1 then 'M'
            else 'S'
            end     as [address_type_cd]
        ,case
            when len(ltrim(rtrim(d.[primary address line 1])))
                + len(ltrim(rtrim(d.[primary address line 2])))
                + len(ltrim(rtrim(d.[primary address line 3]))) 
                + len(ltrim(rtrim(d.[primary address line 4]))) > 3 
            then ltrim(rtrim(d.[primary address line 1]))
                + ltrim(rtrim(d.[primary address line 2]))
                + ltrim(rtrim(d.[primary address line 3]))
                + ltrim(rtrim(d.[primary address line 4]))
            end     as [addr_1]

        ,case
            when len(ltrim(rtrim(d.[statement mailing address line 1])))
                + len(ltrim(rtrim(d.[statement mailing address line 2])))
                + len(ltrim(rtrim(d.[statement mailing address line 3]))) 
                + len(ltrim(rtrim(d.[statement mailing address line 4]))) > 3 
            then ltrim(rtrim(d.[statement mailing address line 1]))
                + ltrim(rtrim(d.[statement mailing address line 2]))
                + ltrim(rtrim(d.[statement mailing address line 3]))
                + ltrim(rtrim(d.[statement mailing address line 4]))
            end     as [addr_2]

        ,case
            when len(ltrim(rtrim(d.[mailing address line 1])))
                + len(ltrim(rtrim(d.[mailing address line 2])))
                + len(ltrim(rtrim(d.[mailing address line 3])))
                + len(ltrim(rtrim(d.[mailing address line 4]))) > 3
            then ltrim(rtrim(d.[mailing address line 1]))
                + ltrim(rtrim(d.[mailing address line 2]))
                + ltrim(rtrim(d.[mailing address line 3]))
                + ltrim(rtrim(d.[mailing address line 4]))
            end     as [addr_3]

        ,case
            when len(ltrim(rtrim(d.[primary city]))) > 1 then ltrim(rtrim(d.[primary city]))
            when len(ltrim(rtrim(d.[statement mailing city]))) > 1 then ltrim(rtrim(d.[statement mailing city]))
            when len(ltrim(rtrim(d.[mailing city]))) > 1 then ltrim(rtrim(d.[mailing city]))
            end     as [city]

        ,case
            when len(ltrim(rtrim(d.[primary state]))) > 1 then ltrim(rtrim(d.[primary state]))
            when len(ltrim(rtrim(d.[statement mailing state]))) > 1 then ltrim(rtrim(d.[statement mailing state]))
            when len(ltrim(rtrim(d.[mailing state]))) > 1 then ltrim(rtrim(d.[mailing state]))
            end     as [state]

        ,case
            when len(ltrim(rtrim(d.[primary zip]))) > 1 then ltrim(rtrim(d.[primary zip]))
            when len(ltrim(rtrim(d.[statement mailing zip]))) > 1 then ltrim(rtrim(d.[statement mailing zip]))
            when len(ltrim(rtrim(d.[mailing zip]))) > 1 then ltrim(rtrim(d.[mailing zip]))
            end     as [zip]

        ,tb_c.country_cd
        ,getdate()  as [batch_dt]
        ,cast([ACCOUNT NUMBER] as varchar) as ACCOUNT_NUMBER
        ,[MODEL_ACCOUNT_ID] as MODEL_ACCOUNT_ID
    from mtb.dbo.deloitteenrichedfile   d       (nolock)
    left join tb_country                tb_c    (nolock)
        on case 
            when len(ltrim(rtrim(d.[primary country]))) > 1 then ltrim(rtrim(d.[primary country]))
            when len(ltrim(rtrim(d.[statement mailing country]))) > 1 then ltrim(rtrim(d.[statement mailing country]))
            when len(ltrim(rtrim(d.[mailing country]))) > 1 then ltrim(rtrim(d.[mailing country]))
            end
                = tb_c.country_name
    where d.application != 'RBC'
    )

    select *,dense_rank() over(partition by account_number,address_type_cd order by [model_account_id]) as [ad_flag] from cte


    )AA 

0 个答案:

没有答案