复杂的查询集成

时间:2013-10-21 19:28:57

标签: sql sql-server tsql

这是我正在处理的主要查询,即将完成。我需要添加的最后一件事是客户端的最后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

我需要手机*列填充最新的电话号码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.是否可以整合这两个查询?第一个是很久以前由另一个人写的,我只需要在其中构建第二个查询。

1 个答案:

答案 0 :(得分: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]