5

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

4 に答える 4

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
于 2012-12-19T11:33:53.623 に答える
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
于 2012-12-19T06:45:59.747 に答える
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    
于 2012-12-19T06:55:45.717 に答える
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のデモ

于 2012-12-19T17:19:55.570 に答える