I have table
Cost RenderedValue SectionName
------------------------------------
100.00 1000.00 Section1
200.00 2000.00 Section2
300.00 3000.00 Section3
400.00 4000.00 Section4
and I want to produce this XML:
<Root>
<Section1Cost>100.00</Section1Cost>
<Section1RenderedValue>1000.00</Section1RenderedValue>
<Section2Cost>200.00</Section2Cost>
<Section2RendredValue>2000.00</Section2RendredValue>
</Root>
I was able to produce it using the TSQL ( Test is my table name)
SELECT
(SELECT Cost AS 'Cost'FROM Test WHERE SectionName = 'Section1') AS 'Section1Cost',
(SELECT RenderedValue AS 'Cost'FROM Test WHERE SectionName = 'Section1') AS 'Section1RenderedValue',
(SELECT Cost AS 'Cost'FROM Test WHERE SectionName = 'Section2') AS 'Section2Cost',
(SELECT RenderedValue AS 'Cost'FROM Test WHERE SectionName = 'Section2') AS 'Section2RendredValue'
FOR XML Path(''), Root('Root')
But this is ugly and I think it is not optimized . Can it be more elegant or whatever I have is correct?
I may have at most 30 rows in that Test table