SQL-如果Row是重复的,则将其作为嵌套重复的新列

时间:2016-12-22 12:37:28

标签: sql sql-server

我有三个表用于获取ID的应用程序,一个用于applicantInfo,一个用于地址 我需要创建一个视图,向我显示每列的数据,但条件是每个应用程序的一行和地址需要它们自己。 我做了这个选择查询:

Select dbo.Application.ApplicationID, dbo.ApplicantInfo.FirstName,dbo.ApplicantInfo.LastName, dbo.Address.AddressID,dbo.Address.City from dbo.Application LEFT JOIN dbo.ApplicantInfo on dbo.Application.ApplicationID = dbo.ApplicantInfo.ApplicationID LEFT JOIN dbo.Address ON dbo.Application.ApplicationID = dbo.Address.ApplicationID 输出如下: <



<table style="width:100%">
  <tr>
    <th>ApplicationID</th>
    <th>FirstName</th> 
    <th>LastName</th>
    <th>AddressID</th> 
    <th>City</th>
  </tr>
  <tr>
    <td>1</td>
    <td>John</td> 
    <td>Zak</td>
    <th>1</th> 
    <th>C1</th>
  </tr>
  <tr>
    <td>1</td>
    <td>John</td> 
    <td>Zak</td>
    <th>2</th> 
    <th>C2</th>
  </tr>
    <tr>
    <td>1</td>
    <td>John</td> 
    <td>Zak</td>
    <th>3</th> 
    <th>C3</th>
  </tr>
    <tr>
    <td>1</td>
    <td>John</td> 
    <td>Zak</td>
    <th>4</th> 
    <th>C4</th>
  </tr>
</table>
&#13;
&#13;
&#13;

但我需要这样的输出:

&#13;
&#13;
<table style="width:100%">
  <tr>
    <th>ApplicationID</th>
    <th>FirstName</th> 
    <th>LastName</th>
    <th>AddressID1</th> 
    <th>City1</th>
    <th>AddressID2</th>
    <th>City2</th>
    <th>AddressID3</th>
    <th>City3</th>
    <th>AddressID4</th>
    <th>City4</th>
  </tr>
  <tr>
    <td>1</td>
    <td>John</td> 
    <td>Zak</td>
    <td>A1</td>
    <td>C1</td>
    <td>A2</td> 
    <td>C2</td>
    <td>A3</td> 
    <td>C3</td>
    <td>4</td> 
    <td>C4</td>
  </tr>
</table>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:2)

如果你不需要DYNAMIC,一个简单的条件聚合就可以解决问题

Select ApplicationID
      ,FirstName
      ,LastName
      ,AddressID1 = max(case when AddressID=1 then concat('A',AddressID) else '' end)
      ,City1      = max(case when AddressID=1 then City else '' end)
      ,AddressID2 = max(case when AddressID=2 then concat('A',AddressID) else '' end)
      ,City2      = max(case when AddressID=2 then City else '' end)
      ,AddressID3 = max(case when AddressID=3 then concat('A',AddressID) else '' end)
      ,City3      = max(case when AddressID=3 then City else '' end)
      -- ... Expand as necessary
 From (
       -- Your Complicated Query
      ) A
 Group By 
       ApplicationID
      ,FirstName
      ,LastName

返回

enter image description here

  

EDIT for Updated Info

注意RN = Row_Number()行

Select ApplicationID
      ,FirstName
      ,LastName
      ,AddressID1 = max(case when RN=1 then concat('A',AddressID) else '' end)
      ,City1      = max(case when RN=1 then City                  else '' end)
      ,AddressID2 = max(case when RN=2 then concat('A',AddressID) else '' end)
      ,City2      = max(case when RN=2 then City                  else '' end)
      ,AddressID3 = max(case when RN=3 then concat('A',AddressID) else '' end)
      ,City3      = max(case when RN=3 then City                  else '' end)
      ,AddressID4 = max(case when RN=4 then concat('A',AddressID) else '' end)
      ,City4      = max(case when RN=4 then City                  else '' end)
      ,AddressID5 = max(case when RN=5 then concat('A',AddressID) else '' end)
      ,City5      = max(case when RN=5 then City                  else '' end)
      ,AddressID6 = max(case when RN=6 then concat('A',AddressID) else '' end)
      ,City6      = max(case when RN=6 then City                  else '' end)
      ,AddressID7 = max(case when RN=7 then concat('A',AddressID) else '' end)
      ,City7      = max(case when RN=7 then City                  else '' end)
      ,AddressID8 = max(case when RN=8 then concat('A',AddressID) else '' end)
      ,City8      = max(case when RN=8 then City                  else '' end)
      ,AddressID9 = max(case when RN=9 then concat('A',AddressID) else '' end)
      ,City9      = max(case when RN=9 then City                  else '' end)
 From (
        Select dbo.Application.ApplicationID
             , dbo.ApplicantInfo.FirstName
             , dbo.ApplicantInfo.LastName
             , dbo.Address.AddressID
             , dbo.Address.City 
             , RN = Row_Number() over (Partition By ApplicationID,FirstName,LastName Order By AddressID) 
         from dbo.Application 
         LEFT JOIN  dbo.ApplicantInfo on dbo.Application.ApplicationID = dbo.ApplicantInfo.ApplicationID 
         LEFT JOIN  dbo.Address ON dbo.Application.ApplicationID = dbo.Address.ApplicationID
      ) A
 Group By 
       ApplicationID
      ,FirstName
      ,LastName

返回

enter image description here

答案 1 :(得分:0)

如果您只想要4个地址行,那么您可以通过简单地连接到地址表4次来避免使用数据透视表。你需要一些方法来识别哪些地址记录对应1,2,3&amp; 4.或者您可以使用row_number()创建它。 EG

with address_numbered as
(select *, row_number() over (partition by applicationId order by addressid) as row_num from address)
Select dbo.Application.ApplicationID, dbo.ApplicantInfo.FirstName,dbo.ApplicantInfo.LastName,
a1.AddressID,a1.City ,
a2.AddressID as AddressID2,a2.City as City 
from dbo.Application LEFT JOIN 
dbo.ApplicantInfo on dbo.Application.ApplicationID = dbo.ApplicantInfo.ApplicationID 
LEFT JOIN 
address_numbered a1 ON dbo.Application.ApplicationID = a1.ApplicationID
and a1.row_num = 1
LEFT JOIN 
address_numbered a2 ON dbo.Application.ApplicationID = dbo.Address.ApplicationID
and a2.row_num = 2

在2次加入后我感到无聊但你明白了......