Wednesday, October 15, 2014

#2 Write a SELECT statement that returns these column names and data from the OrderItems table:



Write a SELECT statement that returns these column names and data from the OrderItems table:
ItemID The ItemID column
ItemPrice The ItemPrice column
DiscountAmount The DiscountAmount column
Quantity The Quantity column
PriceTotal A column that’s calculated by multiplying the item price with the quantity
DiscountTotal A column that’s calculated by multiplying the discount amount with the quantity
ItemTotal A column that’s calculated by subtracting the discount amount from the item price and then multiplying by the quantity
Only return rows where the ItemTotal is greater than 500.
Sort the result set by item total in descending sequence.

Answer:
 SELECT
ItemID, ItemPrice, DiscountAmount, Quantity,
ItemPrice * Quantity AS PriceTotal,
DiscountAmount * Quantity AS DiscountTotal,
(ItemPrice - DiscountAmount) * Quantity
AS ItemTotal
FROM OrderItems
WHERE (ItemPrice - DiscountAmount) * Quantity > 500
ORDER BY ItemTotal DESC;

2 comments:

  1. Hi Chester , thank you for you blog it is very helpful, I have a question for u a s a beginner in SQL lang:
    Why did u declare all The ItemTotal at the end of the code and not wrote the formula ? I tried it and worked, what is the reason behind that? (last line and before last line)
    Thank you

    ReplyDelete
    Replies
    1. The ItemTotal field is declared in the fifth line with: (ItemPrice-Discount)*Quantity AS ItemTotal

      Delete