2013-10-21 88 views
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 

我需要的電話*列與最近的電話號碼是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.是否有可能整合這兩個查詢?第一個是很久以前另一個人寫的,我只需要在其中創建第二個查詢。

+0

查找一個版本'pivot' – Laurence

+0

如果有是兩個查詢中的常見id列,那麼我認爲你可以做一些事情,比如yourSecondQuery中的yourIDColumn,可能是什麼? – Andrew

+0

@Laurence似乎不同的是沒有必要,因爲它返回2165行有或沒有獨特的帳戶。在p.pid上有一個組不能被添加。 –

回答

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] 
+0

Drats,我必須調整數據透視表的兼容性級別。 Lemme看看網絡人員是否可以做到這一點。 –

+0

@JamesWilson請不要這樣做,如果它是一個現場系統,你可以用手寫它們,它只是多吸一點 – Laurence

+0

我不打算這樣做。我剛剛檢查過,我的老闆說要用臨時表格手寫它們。抱歉,所有的大驚小怪。 –