Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
SergioM
Posting Yak Master
170 Posts |
Posted - 2011-08-30 : 12:57:33
|
I'm pretty sure that this is doable (in fact easy), but I don't know what to search for. Let's say I have a table with 3 columns. "Item", "Quantity" & "Cost". I want to write a SQL Query that will output 4 columns, but I don't want to change anything in the hard-written table. The fourth column is a calculation which is "Amount invested". "Quantity" multiplied by "Cost". And the output would look something like this:Item Quantity Cost Amount InvestedSocks 7 2.50 17.5Shirts 15 7.50 112.5Slacks 12 22 264Can anyone point me to the write direction? So far, I'm working with filtering commands like "Select" "From" "where" and "order". But nothing that would change the data or output. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-08-30 : 13:00:55
|
[code]SELECT Item,Quantity,Cost,Quantity * Cost AS [Amount Invested]FROM Table[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
|
|
craigwg
Posting Yak Master
154 Posts |
Posted - 2011-08-30 : 13:24:13
|
Above query is perfect. It's called an aggregated column. You are essentially creating a column that is aggregated from other values. In this case those values are cells from the table, but they can be static numbers or values from other locations in the database (or even elsewhere on the server...or even elsewhere from a linked server!). You don't HAVE to give the column an alias but it can be helpful. I personally would recommend not using spaces in the alias, but there is a time and a place for this.Craig Greenwood |
|
|
SergioM
Posting Yak Master
170 Posts |
Posted - 2011-08-30 : 13:39:28
|
Wow, that was easy! Thanks guys!@Craig, In regards to linking to external files & databases; that is exactly where I am headed. But I am taking baby steps. The next step (for me) is in conditional logic. I'm going to put up a separate post on book suggestions, but I'm really going to need one. Thanks again |
|
|
|
|
|
|
|