Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Select query with FOR XML clause

Posted on 2009-07-01
2
Medium Priority
?
206 Views
Last Modified: 2012-05-07
I am trying to create an XLM file with nodes.  The data is in two tables a Header and Detail table. I have combined those to make a view that looks like.

Customer      CustomerPO OrderDate     StockCode    OrderQty
BOB              10                 01-Jul-09       112-1            5
BOB              10                 02-Jul-09       111-1            3
BOB              10                 03-Jul-09       118-1            2
BOB              10                 04-Jul-09       114-1            1
FRED             NEW           05-Jul-09       118-1            6
FRED             NEW           06-Jul-09       114-1            2
...                   ...                  ...                   ...                 ...

Is there a way to use the FOR XML in SQLServer to get the data to look like the xml below?  Or is there some other utility I could use.

<Orders>
  <OrderHeader>
    <Customer>BOB</Customer>
    <CustomerPoNumber>10</CustomerPoNumber>
    <OrderDate>2009-07-01</OrderDate>
  </OrderHeader>
  <OrderDetails>
    <StockLine>
      <StockCode>112-1</StockCode>
      <OrderQty>5.000</OrderQty>
    </StockLine>
    <StockLine>
      <StockCode>111-1</StockCode>
      <OrderQty>3.000</OrderQty>
    </StockLine>
    <StockLine>
      <StockCode>118-1</StockCode>
      <OrderQty>2.000</OrderQty>
    </StockLine>
    <StockLine>
      <StockCode>114-1</StockCode>
      <OrderQty>1.000</OrderQty>
    </StockLine>
  </OrderDetails>
  <OrderHeader>
    <Customer>FRED</Customer>
    <CustomerPoNumber>FRED</CustomerPoNumber>
    <OrderDate>2009-07-01</OrderDate>
  </OrderHeader>
  <OrderDetails>
    <StockLine>
      <StockCode>118-1</StockCode>
      <OrderQty>6.000</OrderQty>
    </StockLine>
    <StockLine>
      <StockCode>114-1</StockCode>
      <OrderQty>2.000</OrderQty>
    </StockLine>
  </OrderDetails>
</Orders>

Open in new window

0
Comment
Question by:vbchewie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 60

Accepted Solution

by:
Kevin Cross earned 1000 total points
ID: 24757846
Something like this:
(you could try doing the conversion from original join as basically you want to get the details as a subquery for xml)
select Customer AS "OrderHeader/Customer"
, CustomerPO AS "OrderHeader/CustomerPO"
, MIN(OrderDate) AS "OrderHeader/OrderDate"
, CAST((SELECT StockCode AS "StockCode", CAST(OrderQty AS MONEY) AS "OrderQty" FROM tbl b WHERE tbl.Customer = b.Customer and tbl.CustomerPO = b.CustomerPO FOR XML PATH('StockLine'), ROOT('OrderDetails')) AS XML)
from tbl
group by Customer, CustomerPO
for xml path('Order'), root('Orders')

Open in new window

0
 
LVL 1

Author Closing Comment

by:vbchewie
ID: 31598931
Perfect! Thank you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
There have been several questions about Large Transaction Log Files in SQL Server 2008, and how to get rid of them when disk space has become critical. This article will explain how to disable full recovery and implement simple recovery that carries…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question