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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How can I reference a value I calculated?

Author  Topic 

eek142
Starting Member

1 Post

Posted - 2012-02-03 : 14:19:41
Hey everyone,

I recently switched over to MS SQL 2008 from Excel. I need to start managing large data sets using SQL because it's much quicker. Right now, I am importing the data from a CSV file into a table. This generates the table I need. I am now trying to perform calculations on this data, similar to what I had done in Excel. How can I get this to work:


SELECT clothing
, price
, amount
, CASE
WHEN price = 0
THEN 0

WHEN price <> 0
THEN CASE
WHEN amount > 20
THEN 200
ELSE 0
END
END AS 'buyprice'

FROM clothing
WHERE designer LIKE 'calvin klein'

GO




I need to reference 'buyprice' later for calculation of another variable. How do I do this? Do I need another select statement? Do I assign it to a variable? I have tried a bunch of things and I can't get it to work. This is for a very large set of data.


Thanks for any help.

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-02-03 : 15:56:40
you need to form a derived table out of it and use


SELECT *,buyprice+ ....
FROM
(
SELECT clothing
, price
, amount
, CASE
WHEN price = 0
THEN 0

WHEN price <> 0
THEN CASE
WHEN amount > 20
THEN 200
ELSE 0
END
END AS 'buyprice'

FROM clothing
WHERE designer LIKE 'calvin klein'


)t


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -