Как объединить несколько строк из одной таблицы в один результат строки? - PullRequest
1 голос
/ 28 января 2020

Я пытаюсь выяснить, как объединить несколько адресов в одну строку без дублирования каких-либо записей.

Таблица компании

|---------------------|------------------|
|      CompanyId      |     Name         |
|---------------------|------------------|
|          1          |     ABC          |
|---------------------|------------------|
|          2          |     XYZ          |
|---------------------|------------------|

Таблица типов адресов

|---------------------|------------------|
|  AddressTypeId      |   AddressType    |
|---------------------|------------------|
|          1          |     Location     |
|---------------------|------------------|
|          2          |     Shipping     |
|---------------------|------------------|
|          3          |     Billing      |
|---------------------|------------------|

CompanyAddress Table

|---------------------|------------------|------------------|
|  CompanyID          |   AddressId      |   AddressTypeId  |
|---------------------|------------------|------------------|
|          1          |     1            |     1            |
|---------------------|------------------|------------------|
|          1          |     2            |     2            |
|---------------------|------------------|------------------|
|          1          |     2            |     3            |
|---------------------|------------------|------------------|

Адресная таблица

|---------------------|------------------|
|     AddressId       |      Address     |
|---------------------|------------------|
|          1          |     123 Main St  |
|---------------------|------------------|
|          2          |     156 Front St |
|---------------------|------------------|

Я хочу получить следующий результат:

|-------------|--------|-------------------|-------------------|------------------|
|  CompanyId  |  Name  |  LocationAddress  |  ShippingAddress  |  BillingAddress  |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | 123 Main St       |  156 Front St     |  156 Front St    |
|-------------|--------|-------------------|-------------------|------------------|

Сначала я думал, что могу сделать несколько соединений на таблица CompanyAddress и Address выглядит следующим образом:

SELECT
     c.CompanyId
     , c.CompanyName
     , la.Address as LocationAddress  
     , sa.Address as ShippingAddress  
     , ba.Address as BillingAddress  
FROM
     Company c
     JOIN CompanyAddress al ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 1
     JOIN CompanyAddress as ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 2
     JOIN CompanyAddress ab ON ca.CompanyID = c.CompanyID an ca.AddressTypeId = 3
     JOIN Address la ON la.AddressId = al.AddressId
     JOIN Address sa ON sa.AddressId = as.AddressId
     JOIN Address ba ON sa.AddressId = ab.AddressId

Но это меня так:

|-------------|--------|-------------------|-------------------|------------------|
|  CompanyId  |  Name  |  LocationAddress  |  ShippingAddress  |  BillingAddress  |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | 123 Main St       |  null             |  null            |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | null              |  156 Front St     |  null            |
|-------------|--------|-------------------|-------------------|------------------|
|  1          |  ABC   | null              |  null             |  156 Front St    |
|-------------|--------|-------------------|-------------------|------------------|

Я понимаю, почему я получаю этот результат. Но я не вижу правильный запрос в моей голове.

1 Ответ

1 голос
/ 28 января 2020

Вы можете сделать условное агрегирование:

select 
    ca.CompanyId,
    ca.CompanyName,
    max(case when at.AddressType = 'Location' then a.Address end) LocationAddress,
    max(case when at.AddressType = 'Shipping' then a.Address end) ShippingAddress,
    max(case when at.AddressType = 'Billing'  then a.Address end) BillingAddress
from Company c
inner join CompanyAddress ca on ca.CompanyId = c.CompanyId
inner join Address a on a.AddressId = ca.AddressId
inner join AddressType at on at.AddressTypeId = ca.AddressTypeId
group by ca.CompanyId , ca.CompanyName
...