2017-08-25 72 views
1

我有一個數據一個SQL視圖:SQL XML分組

+-----------+-------------+--------------+--------------------+------------------+-----------+----------------+--------------+-------------+----------+ 
| CarparkNo | CarparkAbbr | CarparkDesig | CountingCategoryNo | CountingCategory | FreeLimit | OccupancyLimit | CurrentLevel | Reservation | Capacity | 
+-----------+-------------+--------------+--------------------+------------------+-----------+----------------+--------------+-------------+----------+ 
|   0 | L72   | Lot 72  |     1 | Short-Term  |  174 |   175 |   87 |   0 |  175 | 
|   0 | L72   | Lot 72  |     2 | Contract   |   0 |    0 |   0 |   0 |  0 | 
|   0 | L72   | Lot 72  |     3 | Total   |  174 |   175 |   87 |   0 |  175 | 
+-----------+-------------+--------------+--------------------+------------------+-----------+----------------+--------------+-------------+----------+ 

我需要做的是尋找一個XML文件中像

<Carparks> 
    <Carpark> 
    <CarparkNo>0</CarparkNo> 
    <CarparkAbbr>L72</CarparkAbbr> 
    <CarparkDesig>Lot 72</CarparkDesig> 
    <CountingCategory> 
     <CountingCategoryNo>1</CountingCategoryNo> 
     <CountingCategory>Short-Term</CountingCategory> 
     <FreeLimit>174</FreeLimit> 
     <OccupancyLimit>175</OccupancyLimit> 
     <CurrentLevel>64</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>175</Capacity> 
    </CountingCategory> 
    <CountingCategory> 
     <CountingCategoryNo>2</CountingCategoryNo> 
     <CountingCategory>Contract</CountingCategory> 
     <FreeLimit>0</FreeLimit> 
     <OccupancyLimit>0</OccupancyLimit> 
     <CurrentLevel>0</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>0</Capacity> 
    </CountingCategory> 
    <CountingCategory> 
     <CountingCategoryNo>3</CountingCategoryNo> 
     <CountingCategory>Total</CountingCategory> 
     <FreeLimit>174</FreeLimit> 
     <OccupancyLimit>175</OccupancyLimit> 
     <CurrentLevel>64</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>175</Capacity> 
    </CountingCategory> 
    </Carpark> 
</Carparks> 

到目前爲止,我有一種說法:

SELECT [CarparkNo] 
    ,[CarparkAbbr] 
    ,[CarparkDesig] 
    ,(SELECT [CountingCategoryNo] 
     ,[CountingCategory] 
     ,[FreeLimit] 
     ,[OccupancyLimit] 
     ,[CurrentLevel] 
     ,[Reservation] 
     ,[Capacity] 
     FOR XML PATH('CountingCategory'), TYPE) 
    FROM [PARK_DB].[dbo].[CPCounting] 
    FOR XML PATH('Carpark'), ROOT('Carparks'), TYPE; 

哪種作品,但使重複<Carpark>標籤,而不是保持在一個分組:

<Carparks> 
    <Carpark> 
    <CarparkNo>0</CarparkNo> 
    <CarparkAbbr>L72</CarparkAbbr> 
    <CarparkDesig>Lot 72</CarparkDesig> 
    <CountingCategory> 
     <CountingCategoryNo>1</CountingCategoryNo> 
     <CountingCategory>Short-Term</CountingCategory> 
     <FreeLimit>174</FreeLimit> 
     <OccupancyLimit>175</OccupancyLimit> 
     <CurrentLevel>82</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>175</Capacity> 
    </CountingCategory> 
    </Carpark> 
    <Carpark> 
    <CarparkNo>0</CarparkNo> 
    <CarparkAbbr>L72</CarparkAbbr> 
    <CarparkDesig>Lot 72</CarparkDesig> 
    <CountingCategory> 
     <CountingCategoryNo>2</CountingCategoryNo> 
     <CountingCategory>Contract</CountingCategory> 
     <FreeLimit>0</FreeLimit> 
     <OccupancyLimit>0</OccupancyLimit> 
     <CurrentLevel>0</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>0</Capacity> 
    </CountingCategory> 
    </Carpark> 
    <Carpark> 
    <CarparkNo>0</CarparkNo> 
    <CarparkAbbr>L72</CarparkAbbr> 
    <CarparkDesig>Lot 72</CarparkDesig> 
    <CountingCategory> 
     <CountingCategoryNo>3</CountingCategoryNo> 
     <CountingCategory>Total</CountingCategory> 
     <FreeLimit>174</FreeLimit> 
     <OccupancyLimit>175</OccupancyLimit> 
     <CurrentLevel>82</CurrentLevel> 
     <Reservation>0</Reservation> 
     <Capacity>175</Capacity> 
    </CountingCategory> 
    </Carpark> 
</Carparks> 

我怎麼去這個格式到所需的規格?該命令在其下運行的帳戶只能讀取系統提供的視圖。

+1

我不明白你的意思。一個標籤是「停車場」,另一個是「停車場」。你重複的意思是什麼? –

+0

所以'Carparks'是根,而'Carpark'是,如果/當我們有多個位置。它應該由「CarparkNo」分組。我不知道該怎麼做。 –

+0

好吧。可能重複:https://stackoverflow.com/questions/18132039/sql-server-two-level-group-by-with-xml-output –

回答

1

您需要GROUP BY。簡單的例子

WITH [CPCounting] AS(
     select 1 as [CarparkNo], 100 [CountingCategoryNo] , 'aa' [CountingCategory] 
     union all 
     select 1 as [CarparkNo], 200 [CountingCategoryNo] , 'bb' [CountingCategory] 
     ) 
SELECT [CarparkNo] 
    ,(SELECT [CountingCategoryNo] 
     ,[CountingCategory] 
     FROM [CPCounting] c2 
     WHERE c2.[CarparkNo] = c1.[CarparkNo] 
     FOR XML PATH('CountingCategory'), TYPE 
    ) 
    FROM [CPCounting] c1 
    GROUP BY [CarparkNo]  
    FOR XML PATH('Carpark'), ROOT('Carparks'), TYPE; 

返回

<Carparks> 
    <Carpark> 
     <CarparkNo>1</CarparkNo> 
     <CountingCategory> 
      <CountingCategoryNo>100</CountingCategoryNo> 
      <CountingCategory>aa</CountingCategory> 
     </CountingCategory> 
     <CountingCategory> 
      <CountingCategoryNo>200</CountingCategoryNo> 
      <CountingCategory>bb</CountingCategory> 
     </CountingCategory> 
    </Carpark> 
</Carparks> 
+0

添加'GROUP BY'和'WHERE'給我'列「PARK_DB。因爲它不是在聚合函數或GROUP BY包含clause.' –

+0

但添加了'CarparkAbbr'和'CarparkDesig'到'GROUP需要BY'工作dbo.CPCounting.CarparkAbbr」在選擇列表中無效!謝謝! –