Comment écrire un transfert avec T-SQL

Si j'ai besoin des données via la syntaxe t-sql transpose Que dois-je faire?

BID ----------------- ID.B1211 50332 50333 50922 50420 50421 50326 50327 

Je veux transposer à

 AID BID ---------- ID.B1211 ID.B1211 50332 ID.B1211 50333 ID.B1211 50922 ID.B1211 50420 ID.B1211 50421 ID.B1211 50326 ID.B1211 50327 

Les données réelles sont comme ceci. Chaque ID de BatchID au premier. Et je veux mettre l'ID à une autre colonne. L'uid est l'identité et continue.

 uid BatchID --------------- 32 ID.B121129029-14 33 P3YDCS50332 34 P3YDCS50333 35 P3YDCS50922 36 P3YDCS50420 37 P3YDCS50421 38 P3YDCS50326 39 P3YDCS50327 40 P3YDCS50329 41 P3YDCS50328 42 P3YDCS50423 43 P3YDCS50422 44 P3YDCS50921 45 P3YDCS50334 46 P3YDCS50337 47 ID.B121115009-14 48 P3YDCSO0206 49 P3YDCSO0215 50 P3YDCSO0201 51 P3YDCSO0205 52 P3YDCSO0204 53 P3YDCSO0214 54 P3YDCSO0198 55 P3YDCSO0197 56 P3YDCSO0213 57 P3YDCSO0212 58 P3YDCSO0211 59 P3YDCSO0202 60 P3YDCSO0200 61 P3YDCSO0199 

 select t.uid, a.batchid bid1, nullif(t.batchid, a.batchid) bid22 from <table> t cross apply (select top 1 batchid from <table> where uid <= t.uid and batchid like 'id%' order by uid desc) a 

Je fais quelques suppositions à propos de vos données (id sera toujours en ordre, BatchID commencera toujours par 'ID.') Mais c'est ce que j'ai trouvé …

 CREATE TABLE #Batch (id INT IDENTITY, BatchID VARCHAR(20)); INSERT INTO #Batch (BatchID) VALUES ('ID.B121129029-14') INSERT INTO #Batch (BatchID) VALUES ('P3YDCS50332') INSERT INTO #Batch (BatchID) VALUES ('P3YDCS50333') INSERT INTO #Batch (BatchID) VALUES ('ID.B121115019-14') INSERT INTO #Batch (BatchID) VALUES ('P3YDCS50329') INSERT INTO #Batch (BatchID) VALUES ('P3YDCS50328') INSERT INTO #Batch (BatchID) VALUES ('P3YDCS50423') INSERT INTO #Batch (BatchID) VALUES ('ID.B121115009-14') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0206') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0215') INSERT INTO #Batch (BatchID) VALUES ('ID.B121115049-14') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0211') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0202') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0200') INSERT INTO #Batch (BatchID) VALUES ('P3YDCSO0199') ; WITH MainBatchIDs AS (SELECT ROW_NUMBER() OVER (ORDER BY id) rownum ,id ,BatchID FROM #Batch b1 WHERE BatchID LIKE 'ID.%' ), IDGroups AS (SELECT A1.id AS FirstID ,A2.id AS NextID ,A1.BatchID FROM MainBatchIDs A1 LEFT JOIN MainBatchIDs A2 ON A1.rownum = A2.rownum - 1 ) SELECT G.BatchID AID ,CASE WHEN G.BatchID = B.BatchID THEN NULL ELSE B.BatchID END BID FROM #Batch B INNER JOIN IDGroups G ON B.id >= G.FirstID AND B.id < G.NextID DROP TABLE #Batch 
 declare @t table (uid int,BatchID varchar(30)) insert into @t Values (32,'ID.B121129029-14'), (33,'P3YDCS50332'), (34,'P3YDCS50333'), (35,'P3YDCS50922'), (36,'P3YDCS50420'), (37,'P3YDCS50421'), (38,'P3YDCS50326'), (39,'P3YDCS50327'), (40,'P3YDCS50329'), (41,'P3YDCS50328'), (42,'P3YDCS50423'), (43,'P3YDCS50422'), (44,'P3YDCS50921'), (45,'P3YDCS50334'), (46,'P3YDCS50337'), (47,'ID.B121115009-14'), (48,'P3YDCSO0206'); Select (Select BatchID from @t where BatchID like ('ID.%') and uid= (select MAX(uid) from @tt where BatchID like ('ID.%') and t.uid<o.uid) ) as AID ,BatchID as BID from @to where not BatchID like ('ID.%') order by uid 
 ;WITH cte AS ( SELECT uid, BatchID, CAST('' AS nvarchar(30)) AS BatchID FROM dbo.test60 WHERE uid = 32 --initial ID UNION ALL SELECT t.uid, CASE WHEN t.BatchID NOT LIKE 'ID.%' THEN c.BatchID ELSE t.BatchID END, CASE WHEN t.BatchID LIKE 'ID.%' THEN '' ELSE t.BatchID END FROM dbo.test60 t JOIN cte c ON t.uid = c.uid + 1 ) SELECT * FROM cte 

Démo sur SQLFiddle