I have the following table layout:
OrderDetails:
ItemID (PK, int, not null)
ItemName (nvarchar(450), null)
OrderID (FK, int, not null)
Discounts (nvarchar(max), null)
The Discounts column is declared nvarchar(max) but the data is really XML - no idea why it wasn't declared as XML. I need to query the table to show the OrderID, ItemName, prorated discounts, all other discounts, and total of prorated + all other discounts. Here is an example of some records.
ItemID ItemName OrderID Discounts
8610 Item 1 4227 SEE XML 4227 BELOW
8615 Item 2 4227 <DocumentElement></DocumentElement> //no discounts for this row
8620 Item 3 9387 SEE XML 9387 BELOW
XML OrderId = 4227:
<DocumentElement>
<DiscountsTable>
<DiscountDisplayName>Bundle A</DiscountDisplayName>
<DiscountValue>6.00</DiscountValue>
</DiscountsTable>
<DiscountsTable>
<DiscountDisplayName>Bundle B</DiscountDisplayName>
<DiscountValue>25.00</DiscountValue>
</DiscountsTable>
</DocumentElement>
XML for OrderId = 9387:
<DocumentElement>
<DiscountsTable>
<DiscountDisplayName>Prorated Discount</DiscountDisplayName>
<DiscountValue>6.45</DiscountValue>
</DiscountsTable>
<DiscountsTable>
<DiscountDisplayName>Bundle A</DiscountDisplayName>
<DiscountValue>5.61</DiscountValue>
</DiscountsTable>
<DiscountsTable>
<DiscountDisplayName>Bundle B</DiscountDisplayName>
<DiscountValue>23.39</DiscountValue>
</DiscountsTable>
</DocumentElement>
So, What I need is a query that will return the ItemID, ItemName, aggregated prorated discounts, aggregated bundled discounts, and a total of the discounts added together (prorated + bundled = total discounts). For the 3 records above, the query result should look like this:
Item ID Item Name Prorated Discounts Other Discounts Total Discounts
8610 Item 1 0.00 31.00 31.00
8615 Item 2 0.00 0.00 0.00
8620 Item 3 6.45 29.00 35.45
I've tried using the value() method but I get an error stating it can not be used with nvarchar(max). How do I parse the XML column to pull aggregated values?