我有一個數據一個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>
我怎麼去這個格式到所需的規格?該命令在其下運行的帳戶只能讀取系統提供的視圖。
我不明白你的意思。一個標籤是「停車場」,另一個是「停車場」。你重複的意思是什麼? –
所以'Carparks'是根,而'Carpark'是,如果/當我們有多個位置。它應該由「CarparkNo」分組。我不知道該怎麼做。 –
好吧。可能重複:https://stackoverflow.com/questions/18132039/sql-server-two-level-group-by-with-xml-output –