2016-12-22 4 views
0

IDを取得するアプリケーションとapplicantInfoを取得するアプリケーションの3つ1つと、 を表示するビューを作成する必要がありますそれぞれの列のデータですが、アプリケーションごとに1つの行があり、アドレスにはそれ自体の横にある必要があるという条件があります。 私はこのクエリを選択しますSQL-行が複製されている場合は、新しい列として重複して入れてください

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

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

は、しかし、私はこのような出力を必要とします

+1

の可能性のある重複した[SQL Serverの動的なPIVOTクエリ?](http://stackoverflow.com/questions/10404348/ – Serg

+0

返事のためSergありがとう、私は左の結合を持っているので、それは私のクエリでそれを必要とします –

答えて

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を行います

お知らせ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

+0

データはA1またはC1として定数ではありませんそれは単にデータベースの都市の名前です住所表から選択 –

+0

@AhmedHossamAlhansy日付?私はあなたのサンプルデータに日付が表示されません –

+0

申し訳ありません、私はデータを意味します*** –

0

あなただけの今までに4つのアドレス行をしたいなら、あなたは、単にに参加してピボットの使用を避けることができますアドレステーブルを4回。どのアドレスレコードが1,2,3 &に対応しているかを特定するには、何らかの方法が必要です。または、row_number()でこれを作成できます。 EG 2が参加するが、あなたのアイデアを取得した後、私は飽きてしまった

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

関連する問題