473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

SQL 'FOR XML' query

Hello All

I am trying to write a SQL 'FOR XML' query that produces an XML block in a specific xml format. The query I have so far is close but I am having problems getting it produce the exact xml format that I need. I hoping someone on here can help me.

Using the following SQL, I populate the table against which, the SQL FOR XML query is run:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. CREATE TABLE PerfTable
  5.     (
  6.  
  7.     ID            INT  NOT NULL,
  8.     Name            VARCHAR(500) NOT NULL,
  9.     P_Performance1        NUMERIC(10,2),
  10.     B_Performance1            NUMERIC(10,2),
  11.     P_Performance2        NUMERIC(10,2),
  12.     B_Performance2            NUMERIC(10,2),
  13.     P_Performance3        NUMERIC(10,2),
  14.     B_Performance3            NUMERIC(10,2)
  15. ); 
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23. insert PerfTable (id,Name,P_Performance1,B_Performance1,P_Performance2,B_Performance2,P_Performance3,B_Performance3)
  24. values (111,'Item1', -0.111,-0.112,            -0.121,-0.122,        -0.131,-0.132)
  25.  
  26. insert PerfTable (id,Name,P_Performance1,B_Performance1,P_Performance2,B_Performance2,P_Performance3,B_Performance3)
  27. values (222,'Item2', -0.211,-0.212,            -0.221,-0.222,        -0.231,-0.232)
  28.  
  29.  
  30. insert PerfTable (id,Name,P_Performance1,B_Performance1,P_Performance2,B_Performance2,P_Performance3,B_Performance3)
  31. values (333,'Item3', -0.311,-0.312,            -0.321,-0.322,        -0.331,-0.332)
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. SELECT    TOP 9 id, Name,
  39.         period as "Period_Performance/@Period",
  40.         F_Perf as "Period_Performance/F_Perf",
  41.         B_Perf as "Period_Performance/B_Perf"
  42. FROM 
  43. (
  44.     SELECT         
  45.                 pt.id,
  46.                 pt.Name,
  47.                 pt.P_Performance1 ,
  48.                 pt.B_Performance1,
  49.                 'WTD' as Period1,
  50.                 pt.P_Performance2 ,
  51.                 pt.B_Performance2,
  52.                 'MTD' as Period3,
  53.                 pt.P_Performance3 ,
  54.                 pt.B_Performance3,
  55.                 'YTD' as Period2
  56.     FROM PerfTable pt
  57. )a
  58. UNPIVOT
  59.     (F_Perf FOR F IN 
  60.         (P_Performance1,P_Performance2,P_Performance3)
  61. )AS Fund_unpvt
  62. UNPIVOT
  63.     (B_Perf FOR B IN 
  64.         (B_Performance1,B_Performance2,B_Performance3)
  65. )AS bmk_unpvt
  66. UNPIVOT
  67.     (period FOR periods IN 
  68.         (Period1,Period2, Period3)
  69. )AS period_unpvt        
  70. WHERE (RIGHT(F,1) =  RIGHT(B,1)) and (RIGHT(F,1) =  RIGHT(periods,1))
  71. FOR XML PATH('Performance')
  72.  
  73.  
  74.  


Then I run the following query:
Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3.  
  4. SELECT    id, Name,
  5.         period as "Period_Performance/@Period",
  6.         F_Perf as "Period_Performance/F_Perf",
  7.         B_Perf as "Period_Performance/B_Perf"
  8. FROM 
  9. (
  10.     SELECT         
  11.                 pt.id,
  12.                 pt.Name,
  13.                 pt.P_Performance1 ,
  14.                 pt.B_Performance1,
  15.                 'WTD' as Period1,
  16.                 pt.P_Performance2 ,
  17.                 pt.B_Performance2,
  18.                 'MTD' as Period3,
  19.                 pt.P_Performance3 ,
  20.                 pt.B_Performance3,
  21.                 'YTD' as Period2
  22.     FROM PerfTable pt
  23. )a
  24. UNPIVOT
  25.     (F_Perf FOR F IN 
  26.         (P_Performance1,P_Performance2,P_Performance3)
  27. )AS Fund_unpvt
  28. UNPIVOT
  29.     (B_Perf FOR B IN 
  30.         (B_Performance1,B_Performance2,B_Performance3)
  31. )AS bmk_unpvt
  32. UNPIVOT
  33.     (period FOR periods IN 
  34.         (Period1,Period2, Period3)
  35. )AS period_unpvt        
  36. WHERE (RIGHT(F,1) =  RIGHT(B,1)) and (RIGHT(F,1) =  RIGHT(periods,1))
  37. FOR XML PATH('Performance')
  38.  
  39.  
  40.  

This query produces the following XML:

Expand|Select|Wrap|Line Numbers
  1. <Performance>
  2.   <id>111</id>
  3.   <Name>Item1</Name>
  4.   <Period_Performance Period="WTD">
  5.     <F_Perf>-0.11</F_Perf>
  6.     <B_Perf>-0.11</B_Perf>
  7.   </Period_Performance>
  8. </Performance>
  9. <Performance>
  10.   <id>111</id>
  11.   <Name>Item1</Name>
  12.   <Period_Performance Period="YTD">
  13.     <F_Perf>-0.12</F_Perf>
  14.     <B_Perf>-0.12</B_Perf>
  15.   </Period_Performance>
  16. </Performance>
  17. <Performance>
  18.   <id>111</id>
  19.   <Name>Item1</Name>
  20.   <Period_Performance Period="MTD">
  21.     <F_Perf>-0.13</F_Perf>
  22.     <B_Perf>-0.13</B_Perf>
  23.   </Period_Performance>
  24. </Performance>
  25. <Performance>
  26.   <id>222</id>
  27.   <Name>Item2</Name>
  28.   <Period_Performance Period="WTD">
  29.     <F_Perf>-0.21</F_Perf>
  30.     <B_Perf>-0.21</B_Perf>
  31.   </Period_Performance>
  32. </Performance>
  33. <Performance>
  34.   <id>222</id>
  35.   <Name>Item2</Name>
  36.   <Period_Performance Period="YTD">
  37.     <F_Perf>-0.22</F_Perf>
  38.     <B_Perf>-0.22</B_Perf>
  39.   </Period_Performance>
  40. </Performance>
  41. <Performance>
  42.   <id>222</id>
  43.   <Name>Item2</Name>
  44.   <Period_Performance Period="MTD">
  45.     <F_Perf>-0.23</F_Perf>
  46.     <B_Perf>-0.23</B_Perf>
  47.   </Period_Performance>
  48. </Performance>
  49. <Performance>
  50.   <id>333</id>
  51.   <Name>Item3</Name>
  52.   <Period_Performance Period="WTD">
  53.     <F_Perf>-0.31</F_Perf>
  54.     <B_Perf>-0.31</B_Perf>
  55.   </Period_Performance>
  56. </Performance>
  57. <Performance>
  58.   <id>333</id>
  59.   <Name>Item3</Name>
  60.   <Period_Performance Period="YTD">
  61.     <F_Perf>-0.32</F_Perf>
  62.     <B_Perf>-0.32</B_Perf>
  63.   </Period_Performance>
  64. </Performance>
  65. <Performance>
  66.   <id>333</id>
  67.   <Name>Item3</Name>
  68.   <Period_Performance Period="MTD">
  69.     <F_Perf>-0.33</F_Perf>
  70.     <B_Perf>-0.33</B_Perf>
  71.   </Period_Performance>
  72. </Performance>
  73.  
  74.  
  75.  



This XML that I need to produce is below:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     <Performance>
  3.       <id>1</id> 
  4.       <Name>Item1</Name> 
  5.       <Period_Performance Period="WTD">
  6.         <F_Perf>-0.11</F_Perf> 
  7.         <B_Perf>-0.11</B_Perf>
  8.       </Period_Performance>
  9.       <Period_Performance Period="YTD">
  10.         <F_Perf>-0.12</F_Perf> 
  11.         <B_Perf>-0.12</B_Perf> 
  12.       </Period_Performance>
  13.       <Period_Performance Period="MTD">
  14.         <F_Perf>-0.13</F_Perf> 
  15.         <B_Perf>-0.13</B_Perf> 
  16.       </Period_Performance>
  17.     </Performance>
  18.  
  19.  

Any help you can give is greatly appreciated.

Thanks
Mar 6 '11 #1
1 1672
This issue has been resolved. Thank you.
Mar 7 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Jay | last post by:
Hi all-- I am trying to execute the following query in MS SQL Server 2000. SELECT 1 AS Tag, NULL as parent, 'x' AS , NULL AS , NULL AS UNION ALL
2
by: Jim Cheng | last post by:
Could I use the XML query language (and with appropriate XML query engine) to update XML files? I am pretty sure about querying, but not about updating. Thanks! Jim
5
by: Geo | last post by:
Hi, Im running into a strange problem here! I have created a linked server on one of my SQL Servers (local server). Lets call the linked server as LS1. I have an sproc which resides on the...
3
by: intrepid_dw | last post by:
Hi, all. I'm trying to become smarter about the use of the FOR XML clause in SQL Server. There's one question I'd like to ask that I've not found directly answered elsewhere, probably due to my...
4
by: Drebin | last post by:
I'm missing some piece of the puzzle. I'm using 'for xml' type queries from SQL2K - and now trying to change the front-end from ADO to C#, I can't quite figure it out.. It seems that I could use...
1
by: Torrent | last post by:
The Following is an Error I am getting using VS.NET 2003. When I bind the XML and XSLT Document together in Internet Explorer it works fine. However when I attempt to Load the File with the...
0
by: Rob | last post by:
I doubt this is the best way to do it, but what I came up with was to hide the XML in an HTML Comment then edit the file deleting the HTML stuff and keep the XML results. If anyone has a better...
2
by: Martin Steffen | last post by:
Hi, XML-Query languages are quite outside my area of expertise. I need, however material for an academic talk, focusing on XML-query languages. So I'm looking for fast entry points to that...
0
by: martin1 | last post by:
Hi, All, I want to generate empty data tag, for example, the table1 has 2 fileds called field1 and field2, the field1 has data there but field2 no data, when creating xml fiel using query: ...
1
by: Cezus | last post by:
Hello, I cannot convert the following query in the dataset to a string. It says it cannot get more then 2034 chars long... the string just ends at 2034 characters... this is where it goes...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.