2013-01-24 78 views
3

如何創建套像數據和準回聯鍵的如何聚集像集SQL

下面的例子示出了原始數據和期望的輸出如何。

DECLARE @Data TABLE (PID INT, CID INT) 

INSERT INTO @Data 
VALUES (1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3), (2, 1), (2, 2), (4, 3), (4, 4), 
     (5, 4), (5, 5), (8, 4), (8, 5), (6, 6), (7, 4), (7, 5), (7, 6) 

在上面的數據,CID組1,2,3比1的不同,2。這些是唯一

我想要關聯

PID 1和3所設定的1,2,3

PID 2,設定1,2

PID 4設置3,4

PID 5和8設置4個,5

的總體目標是返回XML看起來類似於:

<Items> 
    <Item> 
    <Hierarchies> 
     <Hierarchy> 
      <CID>1</CID> 
      <CID>2</CID> 
      <CID>3</CID> 
     </Hierarchy> 
    </Hierarchies> 
    <PIDs> 
     <PID>1</PID> 
     <PID>3</PID> 
    </PIDs> 
    </Item> 
    <Item> 
    <Hierarchies> 
     <Hierarchy> 
      <CID>1</CID> 
      <CID>2</CID> 
     </Hierarchy> 
    </Hierarchies> 
    <PIDs> 
     <PID>2</PID> 
    </PIDs> 
    </Item> 
    <Item> 
    <Hierarchies> 
     <Hierarchy> 
      <CID>3</CID> 
      <CID>4</CID> 
     </Hierarchy> 
    </Hierarchies> 
    <PIDs> 
     <PID>4</PID> 
    </PIDs> 
    </Item> 
    <Item> 
     ... Etc ... 
    </Item> 
<Items> 
+2

此時...咖啡因。嘗試使用PIVOT(我不太瞭解)並創建基於遊標的瘋狂邏輯。現在,我只是迷失了。我可以創建XML,聚合就是我陷入困境的地方。 –

+4

您是否嘗試過放下鍵盤,拔出鉛筆和紙張,並試圖描述一步一步的算法,將您從輸入轉換爲輸出? – mellamokb

+3

你能用簡單的英語解釋你正在努力完成的任務嗎?如果你不能,你永遠不會到達那裏。 –

回答

4

這樣的事情?或者我誤解了?

DECLARE @Data TABLE (PID INT, CID INT) 

    INSERT INTO @Data 
    VALUES (1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3), (2, 1), (2, 2), (4, 3), (4, 4), 
      (5, 4), (5, 5), (8, 4), (8, 5), (6, 6), (7, 4), (7, 5), (7, 6) 

    with cid_xml as ( select P.PID 
           ,OA.CID 
         from @Data P 
         outer apply (select CAST((select C.CID 
                from @Data C 
                where C.PID = P.PID 

                for xml path('')) as varchar(max)) as CID 
            ) OA 
        ) 
    select cast((select cast(C2.CID as xml) as 'hierarchies/hierarchy' 
         ,cast(OA2.PID as xml) as 'PIDs' 
       from cid_xml C2 
       outer apply (select CAST((select C3.PID 
              from cid_xml C3 
              where C3.CID = C2.CID 
              group by C3.PID 
              for xml path('')) as varchar(max)) as PID 
          )OA2 
       group by C2.CID,OA2.PID 
       order by LEN(C2.CID) desc 
       for xml path('item')) as xml) 
    for xml path('Items') 

我只是做了爲了通過看項目/等級/分級的第一個節點,你自己的目標結果的樣品中沒有

,我得到的結果集是

<Items> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>1</CID> 
      <CID>2</CID> 
      <CID>3</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>1</PID> 
     <PID>3</PID> 
     </PIDs> 
    </item> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>4</CID> 
      <CID>5</CID> 
      <CID>6</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>7</PID> 
     </PIDs> 
    </item> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>3</CID> 
      <CID>4</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>4</PID> 
     </PIDs> 
    </item> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>4</CID> 
      <CID>5</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>5</PID> 
     <PID>8</PID> 
     </PIDs> 
    </item> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>1</CID> 
      <CID>2</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>2</PID> 
     </PIDs> 
    </item> 
    <item> 
     <hierarchies> 
     <hierarchy> 
      <CID>6</CID> 
     </hierarchy> 
     </hierarchies> 
     <PIDs> 
     <PID>6</PID> 
     </PIDs> 
    </item> 
    </Items> 

沒關係?

+0

看起來像做了詭計!謝謝你教育我和你的幫助!將這個應用到我的解決方案中來確保它合適。 –

+0

工作得很好。再次感謝你,弗雷德裏克。 –

+0

歡迎您,alex! – Frederic