![](/img/trans.png)
[英]sql query to select records in a specific format (using pivot or other)
[英]Select output query with specific format using SQL Server
如果可能,我需要一個 T-SQL 查詢,該查詢將從列中返回特定格式的值。
我有兩個表:
第一個表是PrimeClosureInformation
表
[這是 dbo.PrimeClosureInformation 表]
第二個表是dbo.Event
:
我確實加入了兩個表來獲取他們的所有數據 Where This condition
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
但它返回這樣的結果:
我需要每個PrimeClosureInformationPK
的格式將所有事件注釋記錄在一條記錄或一行中,就像這個例子
PrimeClosureInformationPK
Invoiced Remarks
PrimeClosureInformationPK
Invoiced Remarks
EventCommentFirst
EventCreationDateTimeFirst
EventCommentSecond
EventCreationDateTimeSecond
等等
這是我用圖像做的連接結果這是我做的連接的結果
SELECT TOP 1000
dbo.PrimeClosureInformation.PrimeClosureInformationPK,
dbo.PrimeClosureInformation.Invoiced,
dbo.PrimeClosureInformation.Remarks,
dbo.Event.EventComment,
dbo.Event.EventCreationDateTime,
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
如果我理解正確,一個 PK 的所有值都應該顯示在一列中,那么您可以嘗試以下操作:
SELECT CONCAT(dbo.PrimeClosureInformation.PrimeClosureInformationPK, '|' , dbo.PrimeClosureInformation.Invoiced, '', dbo.PrimeClosureInformation.Remarks, '|'
dbo.Event.EventComment, '|'
dbo.Event.EventCreationDateTime
FROM
dbo.PrimeClosureInformation
INNER JOIN
dbo.Event ON dbo.Event.EntityID = dbo.PrimeClosureInformation.PrimeClosureInformationPK
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.