如何修复此数据透视查询...?

时间:2014-04-03 10:22:39

标签: sql sql-server pivot

我有这个问题:

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

以这种方式给我带来数据:

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

由于某种原因,这些联系人位于不同的行中且仅在列中:&#34; contact_1&#34;。 但是我需要每个公司名称只有一行,并且联系人有5个不同的列。像这样:

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

有人可以告诉我如何修复此查询以便根据需要为我提供数据......?

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

1 个答案:

答案 0 :(得分:1)

您遇到的问题是使用以下行:

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

这是为contacts表格中的每一行创建唯一编号,但您要按id列对表格进行分区,该列似乎是每行的唯一值。

您应该根据contact_company_relation表中存在的行数对数据进行分区。

我也会将代码更改为以下内容:

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;

SQL Fiddle with Demo。这给出了一个结果:

| 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) |