我覺得可能是在這裏築巢的不必要的水平,但這應該工作:
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]
查找一個版本'pivot' – Laurence
如果有是兩個查詢中的常見id列,那麼我認爲你可以做一些事情,比如yourSecondQuery中的yourIDColumn,可能是什麼? – Andrew
@Laurence似乎不同的是沒有必要,因爲它返回2165行有或沒有獨特的帳戶。在p.pid上有一個組不能被添加。 –