如果我需要通过t-sql语法转置数据 我该怎么办?
BID
-----------------
ID.B1211
50332
50333
50922
50420
50421
50326
50327
我想转置
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
真实数据就像这样。每个BatchID的ID都是第一个。 我想把ID放到另一列。这是一个身份和连续的。
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
答案 0 :(得分:2)
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
答案 1 :(得分:1)
我对你的数据做了一些假设(id总是按顺序排列,BatchID总是以'ID'开头。)但这就是我想出来的......
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
答案 2 :(得分:1)
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 @t t where BatchID like ('ID.%') and t.uid<o.uid) ) as AID
,BatchID as BID from @t o where not BatchID like ('ID.%')
order by uid
答案 3 :(得分:0)
;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
SQLFiddle上的演示