0

这是我正在处理的主要查询,即将完成。我需要添加的最后一件事是客户的最后 10 个电话号码。

    select distinct
        max(case when isnull(d.lastname,'')='' then d.name 
            else d.firstname+' '+(case when isnull(d.middlename,'')='' then '' else d.middlename+' ' end)+d.lastname+
                (case when isnull(d.suffix,'')='' then '' else ' '+d.suffix end) end)
        ,max(isnull(d.street1,p.street1))
        ,max(isnull(d.street2,''))
        ,max(d.city)
        ,max(isnull(d.state,p.state))
        ,max(isnull(d.zipcode,p.zipcode))
        ,max(dbo.stripnondigits(isnull(d.ssn,p.ssn)))
        ,'Add'
        ,'Primary Secondary Flag'
        ,max(m.number)
        , as 'phone1'
        , as 'phone2'
        , as 'phone3'
        , as 'phone4'
        , as 'phone5'
        , as 'phone6'
        , as 'phone7'
        , as 'phone8'
        , as 'phone9'
        , as 'phone10'
    from people p
    inner join master m
        on p.accountid = m.number
    inner join d_table d on p.debtorid=d.debtorid and isnull(d.jobname,'')=''
group by p.pid

我需要 phone* 列来填充最近的电话号码是phone1下一个最近的号码phone2,依此类推。

这是获取最后 10 个电话号码的查询:

select ah.rownumber, ah.number, ah.dateadded, ah.phonenumber
from (SELECT ROW_NUMBER() OVER (partition by number order by dateadded desc) as 'rowNumber', number, dateadded, phonenumber
            FROM phones_master) ah
where rownumber <=10 
order by ah.number, ah.rownumber

此查询将返回一个人最多(1 到 10)个电话号码。

它按 ah.number = m.number 分组

我不确定如何将这两个查询合并为一个。有些帐户会返回 1 个电话号码,有些可能会返回 10 个。甚至可以整合这两个查询吗?第一个是很久以前由另一个人写的,我只需要在其中构建第二个查询。

4

1 回答 1

1

我认为这里可能存在不必要的嵌套级别,但这应该有效:

Select
    name,
    street1,
    street2,
    city,
    state,
    zipcode,
    ssn,
    action,
    flag,
    [number],
    [1] phone1,
    [2] phone2,
    [3] phone3,
    [4] phone4,
    [5] phone5,
    [6] phone6,
    [7] phone7,
    [8] phone8,
    [9] phone9,
    [10] phone10
From (
    select
        [name],
        street1,
        street2,
        city,
        state,
        zipcode,
        ssn,
        action,
        flag,
        p.[number],
        t.rn,
        t.phonenumber
    from (
        select
            max(case when 
                    isnull(d.lastname, '') = '' then d.name 
                    else d.firstname + ' ' + (
                    case when 
                        isnull(d.middlename, '') = '' then '' 
                        else d.middlename + ' ' 
                    end) + d.lastname + (
                    case when
                        isnull(d.suffix, '') = '' then '' 
                        else ' ' + d.suffix 
                    end) 
                end) [name],
            max(isnull(d.street1, p.street1)) street1,
            max(isnull(d.street2,'')) street2,
            max(d.city) city,
            max(isnull(d.state,p.state)) state,
            max(isnull(d.zipcode,p.zipcode)) zipcode,
            max(dbo.stripnondigits(isnull(d.ssn,p.ssn))) ssn,
            'Add' action,
            'Primary Secondary Flag' flag,
            max(m.number) [number]
        from
            people p
                inner join 
            master m
                on p.accountid = m.[number]
                inner join 
            d_table d 
                on p.debtorid = d.debtorid
        where
            isnull(d.jobname, '') = ''
        group by 
            p.pid
        ) p
        left outer join (
            select 
                ah.[number], 
                ah.rn, 
                ah.phonenumber
            from (
                select 
                    [number],
                    row_number() over (partition by [number] order by dateadded desc) rn, 
                    phonenumber
                from
                    phones_master
                ) ah
            where 
                rn <= 10 
        ) t
        on p.[number] = t.[number]
    ) x
pivot (
    max(phonenumber)
for
    rn in ([1], [2], [3], [4], [5], [6], [7], [8], [9], [10])
) as piv

这是旧兼容级别的版本

Select
    name,
    street1,
    street2,
    city,
    state,
    zipcode,
    ssn,
    action,
    flag,
    [number],
    max(case rn when 1 then phonenumber end) phone1,
    max(case rn when 2 then phonenumber end) phone2,
    max(case rn when 3 then phonenumber end) phone3,
    max(case rn when 4 then phonenumber end) phone4,
    max(case rn when 5 then phonenumber end) phone5,
    max(case rn when 6 then phonenumber end) phone6,
    max(case rn when 7 then phonenumber end) phone7,
    max(case rn when 8 then phonenumber end) phone8,
    max(case rn when 9 then phonenumber end) phone9,
    max(case rn when 10 then phonenumber end) phone10
From (
    select
        [name],
        street1,
        street2,
        city,
        state,
        zipcode,
        ssn,
        action,
        flag,
        p.[number],
        t.rn,
        t.phonenumber
    from (
        select
            max(case when 
                    isnull(d.lastname, '') = '' then d.name 
                    else d.firstname + ' ' + (
                    case when 
                        isnull(d.middlename, '') = '' then '' 
                        else d.middlename + ' ' 
                    end) + d.lastname + (
                    case when
                        isnull(d.suffix, '') = '' then '' 
                        else ' ' + d.suffix 
                    end) 
                end) [name],
            max(isnull(d.street1, p.street1)) street1,
            max(isnull(d.street2,'')) street2,
            max(d.city) city,
            max(isnull(d.state,p.state)) state,
            max(isnull(d.zipcode,p.zipcode)) zipcode,
            max(dbo.stripnondigits(isnull(d.ssn,p.ssn))) ssn,
            'Add' action,
            'Primary Secondary Flag' flag,
            max(m.number) [number]
        from
            people p
                inner join 
            master m
                on p.accountid = m.[number]
                inner join 
            d_table d 
                on p.debtorid = d.debtorid
        where
            isnull(d.jobname, '') = ''
        group by 
            p.pid
        ) p
        left outer join (
            select 
                ah.[number], 
                ah.rn, 
                ah.phonenumber
            from (
                select 
                    [number],
                    row_number() over (partition by [number] order by dateadded desc) rn, 
                    phonenumber
                from
                    phones_master
                ) ah
            where 
                rn <= 10 
        ) t
        on p.[number] = t.[number]
    ) x
Group By
    name,
    street1,
    street2,
    city,
    state,
    zipcode,
    ssn,
    action,
    flag,
    [number]
于 2013-10-21T20:35:21.817 回答