|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, March 04, 2010 12:19 AM
Points: 94,
Visits: 60
|
|
I want to Use calculated column name in query again in the same query.
For eg:
select productname, (uprice+sprice) As pprice, CASE WHEN IsNull(pprice) Then '0' Else pprice End As pprice2 from products
Here pprice is the value I am calculating and want to be able to refer it in the same query without having to write the calculation again. This is just a simple example but the actual calculated column is complicated and very long and it makes it very difficult to debug if I have to use the same code again and again. Is there a cleaner way to do this in same query?
|
|
|
|
|
SSCarpal Tunnel
       
Group: General Forum Members
Last Login: Today @ 2:19 PM
Points: 4,324,
Visits: 9,665
|
|
This sounds like a pure SQL question, not SSIS? Anyhow, there is a way, using Common Table Expressions (or CTEs).
Using your example, it would be something like:
With Data as (select productname, (uprice+sprice) As pprice from xxxxx) select data.productname, CASE WHEN IsNull(data.pprice) Then '0' Else data.pprice End As pprice2 from data
____________________________________________________________________________________________
Help us to help you. For better, quicker and more focused answers to your questions, consider following the advice in this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
If you are asking for help and your post does not contain a question, you should expect responses which do not contain any answers. Put a question mark in there somewhere - it's not rocket science.
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, March 04, 2010 12:19 AM
Points: 94,
Visits: 60
|
|
Thanks but this seems like it will execute the first query to get the column values and then execute the main query again which will take much longer than executing it as a single query. There is no way to do it without using a new table?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: 2 days ago @ 8:45 PM
Points: 7,002,
Visits: 13,999
|
|
Phil is showing you what is called a Common Table Expression. Unless you put that calculated column in your table, that's the way to do what you are asking.
You should try it out - how it's written often has very little to do with exactly how it executes. Don't assume it will necessarily do a two-pass query.
---------------------------------------------------------------------------------- Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, April 29, 2010 2:48 AM
Points: 124,
Visits: 275
|
|
For getting faster response, please post readily consumable sample script.
for now i have created a table with dummy data and calling the calculated price column at multiple places in the select statement by actually calculating only once.
create table MultiUSe (Pid int,productname varchar(10),Uprice int,SPrice int) insert into MultiUSe values (1,'aaa',10,20) insert into MultiUSe values(2,'bbb',30,40) insert into MultiUSe values(3,'ccc',50,60) insert into MultiUSe values(4,'ddd',70,80) insert into MultiUSe values(5,'eee',90,100) insert into MultiUSe (Pid,Productname) values(6,'fff')
With DataCTE as (select productname, (uprice+sprice) As pprice from MultiUSe) select productname, pprice 'pp1',pprice 'pp2',pprice 'pp3' from DataCTE
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Wednesday, June 12, 2013 12:04 PM
Points: 6,739,
Visits: 12,167
|
|
|
|
|