문제

I have this query:

with cte1 as (
select id,
       row_number() over (partition by [Id] order by id) as row, 
       first_name + ' ' + last_name as [Contact Name] 
from contacts
where is_company = 0 and is_active = 1
),
companyContacts as (
 select * from cte1 where row < 6
)

select company.company_name, 
   c.[1] as contact_1,
   c.[2] as contact_2,
   c.[3] as contact_3,
   c.[4] as contact_4,
   c.[5] as contact_5

 from contacts company
 left join contact_company_relation_additional_information relation 
    on company.id = relation.company_id and     relation.ContactCompanyRelation_IsActive = 1
    left outer join
 (select * 
 from companyContacts
 pivot (min([Contact Name]) for row in ([1],[2],[3],[4],[5])) x
) c on c.id = relation.contact_id

where is_company = 1 and is_active = 1
order by company.company_name

That brings me the data this way:

company_name    contact_1   contact_2   contact_3   contact_4       contact_5
Analist         Ori Reshef  NULL        NULL        NULL            NULL
Analist         Ben Gurion  NULL        NULL        NULL            NULL
Analist         Ofer Jerus  NULL        NULL        NULL            NULL
Bar Net         Maya Leshe  NULL        NULL        NULL            NULL
Bar Net         Yossi Farc  NULL        NULL        NULL            NULL
Bar Net         Dima Brods  NULL        NULL        NULL            NULL

Here for some reason the contacts are in different rows and only in column: "contact_1". But I need to have only one row ro each company name, and have the contacts in 5 diferent columns. Like this:

company_name    contact_1   contact_2   contact_3   contact_4       contact_5
Analist         Ori Reshef  Ben Gurion  Ofer Jerus  NULL            NULL
Bar Net         Maya Leshe  Yossi Farc  Dima Brods  NULL            NULL

Can someone tell me how can I fix this query to bring me the data as I need it...?

My table struct is:
table Contacts: [id], [is_company], [first_name], [last_name], [company_name]
table contact_company_relation: [id], [company_id], [contact_id]

Sample Data:
table contacts:
id  is_company   first_name   last_name   company_name
1   True         NULL         NULL        Analist
2   True         NULL         NULL        Bar Net
3   False        Ori          Reshef      NULL
4   False        Ben          Gurion      NULL
5   False        Ofer         Jerus       NULL
6   False        Maya         Leshe       NULL
7   False        Yossi        Farc        NULL
8   False        Dima         Brods       NULL

table contact_company_relation:
id   company_id   contact_id
1    1            3
2    1            4
3    1            5
4    2            6
5    2            7
6    2            8
도움이 되었습니까?

해결책

The problem that you are having is with the following line:

row_number() over (partition by [Id] order by id) as row,

This is creating a unique number for each row in your contacts table but you are partitioning the table by the id column which appears to be a unique value already for each row.

You should be partitioning the data based on the number of rows that exist in the contact_company_relation table instead.

I also would alter the code to something like the following:

select company_name,
  Contact1, Contact2, Contact3,
  Contact4, Contact5
from
(
  select c.first_name + ' ' + c.last_name as contact_name,
    comp.company_name,
    'Contact'+
      cast(row_number() over(partition by ccr.company_id 
                              order by ccr.contact_id) as varchar(1)) row
  from contacts c
  inner join contact_company_relation ccr
    on c.id = ccr.contact_id
  inner join contacts comp
    on ccr.company_id = comp.id
) d
pivot
(
  max(contact_name)
  for row in (Contact1, Contact2, Contact3,
              Contact4, Contact5)
) p;

See SQL Fiddle with Demo. This gives a result:

| COMPANY_NAME |   CONTACT1 |   CONTACT2 |   CONTACT3 | CONTACT4 | CONTACT5 |
|--------------|------------|------------|------------|----------|----------|
|      Analist | Ori Reshef | Ben Gurion | Ofer Jerus |   (null) |   (null) |
|      Bar Net | Maya Leshe | Yossi Farc | Dima Brods |   (null) |   (null) |
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top