如何在SQL中聚合类似的集合

时间:2013-01-24 15:00:11

标签: sql sql-server sql-server-2008

如何创建类似数据的并关联回链接密钥。

下面的示例显示了原始数据和所需输出的方式。

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>

1 个答案:

答案 0 :(得分: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>

没关系?