Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 7129

This novice is having trouble with the AS clause...

$
0
0

I appreciate any help I can get.  As you will probably be able to tell when I ask my question, I am a complete newbie when it comes to SQL.  I just started a class in Database Design and I am already stuck. 

Anyway, my question is this: why am I getting an error message when I execute my query?  I hope I am providing all the necessary information.  This is my SELECT statement:

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

And this is the question asked of me:

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.

I can tell the issue is with the ItemTotal column I created in my SELECT statement, but I don't know how to fix it.  I could very well be wrong, but I thought that when I declared a temporary column using the AS clause, that I could use the title of that column for the rest of the statement.  I don't know how to post the database I am using on here, so I hope you don't need that.  Oh, but when I execute the statement, the message says that the ItemTotal column doesn't exist.  And when I comment out the WHERE clause in my statement, it works fine.

Sorry for the long winded question and I eagerly away any help I can get.  Thanks!


Viewing all articles
Browse latest Browse all 7129

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>