我认为这里可能存在不必要的嵌套级别,但这应该有效:
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]