Solved

SQL XML For XML

Posted on 2011-02-16
5
336 Views
Last Modified: 2012-08-14
I need help creating a for xml path that looks like below...I have an outer node and then an inner node...See below for my desired output and regular SQL statement..<promotionInformation>
 is the outermost node and then <expandedPromotionsInformation> is the inner node..

<promotionInformation>
	  <expandedPromotionsInformation>		<promotionType>UserViewed_Value</promotionType>	<listingAddress>AddressLine1</listingAddress>		<promotionStartDate>DateStartPromotion</promotionStartDate>		<promotionEndDate>DateEndPromotion</promotionEndDate>
	</expandedPromotionsInformation>
 <summaryPromotionsInformation>
		<promotionType> UserViewed_Value </promotionType>
		<promotionEndDate> DateEndPromotion </promotionEndDate>
 </summaryPromotionsInformation>
</promotionInformation>



Select UserViewed_Value, AddressLine1 DateStartPromotion, DateEndPromotion
from Promotion

Open in new window

0
Comment
Question by:cheryl9063
[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
  • 3
  • 2
5 Comments
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34909794
Where does "summaryPromotionsInformation" fit?
0
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 500 total points
ID: 34909814
If I read correctly, then

select
	UserViewed_Value 'expandedPromotionsInformation/promotionType',
	AddressLine1 'expandedPromotionsInformation/listingAddress',
	DateStartPromotion 'expandedPromotionsInformation/promotionStartDate',
	DateEndPromotion 'expandedPromotionsInformation/promotionEndDate',
	UserViewed_Value 'summaryPromotionsInformation/promotionType',
	DateEndPromotion 'summaryPromotionsInformation/promotionEndDate'
from Promotion
for xml path(''), root ('promotionInformation')

Open in new window

0
 
LVL 1

Author Comment

by:cheryl9063
ID: 34910520
I attached the real code below..

The result looks like this
<promotionInformation>
<expandedPromotionsInformation>
<UserViewed_Value>Open House</UserViewed_Value>
<promotionType>OpenHouse</promotionType>
<listingAddress>425 KENMORE Avenue</listingAddress>
</expandedPromotionsInformation>
</promotionInformation>

But I want this

<promotionInformation>
  <expandedPromotionsInformation>
    <promotionType>OpenHouse</promotionType>
    <listingAddress>425 KENMORE Avenue</listingAddress>
    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>
  </expandedPromotionsInformation>
 <SummaryPromotionsInformation>
      <promotionType>OpenHouse</promotionType>
       <PromotionEndDate>null</PromotionEndDate>
 </SummaryPromotionsInformation>
 </promotionInformation>       
Select pt.UserViewed_Value'expandedPromotionsInformation/UserViewed_Value',
pt.PromotionType'expandedPromotionsInformation/promotionType',
l.ListingAddressLine1'expandedPromotionsInformation/listingAddress',
DATENAME(w,lp.DateStartPromotion)+','+ DATENAME(month,lp.DateStartPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateStartPromotion))'expandedPromotionsInformation/promotionStartDate',
DATENAME(w,lp.DateEndPromotion)+','+ DATENAME(month,lp.DateEndPromotion)+ ' ' + convert(varchar(25),DATEPART(MONTH,lp.DateEndPromotion))'expandedPromotionsInformation/promotionEndDate'
from Listing l
join promotiontype pt on l.ListingID = pt.PromotionTypeID
join ListingPromotion lp on l.ListingID = lp.ListingPromotionID
 where l.listingID = 100000
for xml path(''), root ('promotionInformation')

Open in new window

0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34912875
First of all, in XML

    <PromotionStartDate>null</PromotionStartDate>
    <PromotionEndDate>null</PromotionEndDate>

That is not valid.  Those would be interpreted downstream as the STRING value NULL.  The database value null is equivalent to the XML empty node, which FOR XML strips out.

Secondly, it would save us both some time to post the original query in the first place.

Lastly, the only trick to making UserViewedValue in two different places is to use it twice in the SELECT clause, with a different path for each , as I have shown in my example.
0
 
LVL 1

Author Closing Comment

by:cheryl9063
ID: 34964414
Thanks!!
0

Featured Post

Tutorials alone can't teach real engineering

So we built better training tools.

-Hands-on Labs
-Instructor Mentoring
-Scenario-Based Tests
-Dedicated Cloud Servers

All at your fingertips. What are you waiting for?

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 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