field in query that needs to be calculated from 3 other fields

field in a query that will be the result of a caluclation on 3 other fields.

I have a field in a query that needs to be calculated from 3 other fields
(K3/I3)*H3

, NULL AS  Sales_Projection_Thru_end_of_term  -- calculation  needs to look at (Sales_From_Start_To_End_Rebate_Date / Months_Completed_RB1) * terms_in_month_RB1

is this possible ?

here is the query
SELECT --CustomerName --  cspt.[Customer  Name]  -- yes CustomerName
      ContractName --  [Master Agreement Name] --yes ContractName
      ,right(clm.BusinessPartnerCode,9) as BusinessPartnerCode --[BP Code] --  businesspartnercode
      ,contract_number --[Master Agreement ID]  --yes  contract_number
      ,RSVP_Dir --[RSVP/Director]  --yes RSVP_Dir
       ,Sales_Manager --yes  Sales_Manager
       ,CR1.Start_Date as Rebate_Start_Date1  
       , CR1.End_Date AS Rebate_End_Date1
       , DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date) AS 'terms_in_month_RB1' --terms in month -  calculation
       , DATEDIFF(MONTH, CR1.Start_Date, getdate()) AS 'Months_Completed_RB1' -- Mon. comp.
      , null as Conversion_Allowance -- conversion allowance   from contract
      ,ctct.[sls_total]   AS Sales_From_Start_To_End_Rebate_Date --Sales from start thru 12/31/18 (or rebate end date)
      , (Sales_From_Start_To_End_Rebate_Date / Months_Completed_RB1) * terms_in_month_RB1  AS  "Sales_Projection_Thru_end_of_term"  -- calculation i'm trying to do


Msg 207, Level 16, State 1, Line 17
Invalid column name 'Sales_From_Start_To_End_Rebate_Date'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'Months_Completed_RB1'.
Msg 207, Level 16, State 1, Line 17
Invalid column name 'terms_in_month_RB1'



Thanks
fordraiders
LVL 3
FordraidersAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

OMC2000Commented:
try this

SELECT --CustomerName --  cspt.[Customer  Name]  -- yes CustomerName
      ContractName --  [Master Agreement Name] --yes ContractName
      ,right(clm.BusinessPartnerCode,9) as BusinessPartnerCode --[BP Code] --  businesspartnercode
      ,contract_number --[Master Agreement ID]  --yes  contract_number
      ,RSVP_Dir --[RSVP/Director]  --yes RSVP_Dir
       ,Sales_Manager --yes  Sales_Manager
       ,CR1.Start_Date as Rebate_Start_Date1   
       , CR1.End_Date AS Rebate_End_Date1
       , DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date) AS "terms_in_month_RB1" --terms in month -  calculation
       , DATEDIFF(MONTH, CR1.Start_Date, getdate()) AS "Months_Completed_RB1" -- Mon. comp.
      , null as Conversion_Allowance -- conversion allowance   from contract
      ,ctct.[sls_total]   AS Sales_From_Start_To_End_Rebate_Date --Sales from start thru 12/31/18 (or rebate end date)
      , (ctct.[sls_total] / DATEDIFF(MONTH, CR1.Start_Date, getdate())) * DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date)   AS  "Sales_Projection_Thru_end_of_term" -- calculation i'm trying to do

Open in new window

SharathData EngineerCommented:
You cannot use the derived columns in the same SELECT. you have to try like this.
SELECT --CustomerName --  cspt.[Customer  Name]  -- yes CustomerName
      ContractName --  [Master Agreement Name] --yes ContractName
      ,right(clm.BusinessPartnerCode,9) as BusinessPartnerCode --[BP Code] --  businesspartnercode
      ,contract_number --[Master Agreement ID]  --yes  contract_number
      ,RSVP_Dir --[RSVP/Director]  --yes RSVP_Dir
       ,Sales_Manager --yes  Sales_Manager
       ,CR1.Start_Date as Rebate_Start_Date1   
       , CR1.End_Date AS Rebate_End_Date1
       , DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date) AS 'terms_in_month_RB1' --terms in month -  calculation
       , DATEDIFF(MONTH, CR1.Start_Date, getdate()) AS 'Months_Completed_RB1' -- Mon. comp.
      , null as Conversion_Allowance -- conversion allowance   from contract
      ,ctct.[sls_total]   AS Sales_From_Start_To_End_Rebate_Date --Sales from start thru 12/31/18 (or rebate end date)
      , (ctct.[sls_total] / DATEDIFF(MONTH, CR1.Start_Date, getdate())) * DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date)  AS  "Sales_Projection_Thru_end_of_term"  -- calculation i'm trying to do

Open in new window

SharathData EngineerCommented:
or, you can try this.
;with cte as (
SELECT --CustomerName --  cspt.[Customer  Name]  -- yes CustomerName
      ContractName --  [Master Agreement Name] --yes ContractName
      ,right(clm.BusinessPartnerCode,9) as BusinessPartnerCode --[BP Code] --  businesspartnercode
      ,contract_number --[Master Agreement ID]  --yes  contract_number
      ,RSVP_Dir --[RSVP/Director]  --yes RSVP_Dir
       ,Sales_Manager --yes  Sales_Manager
       ,CR1.Start_Date as Rebate_Start_Date1   
       , CR1.End_Date AS Rebate_End_Date1
       , DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date) AS 'terms_in_month_RB1' --terms in month -  calculation
       , DATEDIFF(MONTH, CR1.Start_Date, getdate()) AS 'Months_Completed_RB1' -- Mon. comp.
      , null as Conversion_Allowance -- conversion allowance   from contract
      ,ctct.[sls_total]   AS Sales_From_Start_To_End_Rebate_Date --Sales from start thru 12/31/18 (or rebate end date)
	  )
select *
      , (Sales_From_Start_To_End_Rebate_Date / Months_Completed_RB1) * terms_in_month_RB1  AS  "Sales_Projection_Thru_end_of_term"  -- calculation i'm trying to do
 from cte

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Price Your IT Services for Profit

Managed service contracts are great - when they're making you money. Yes, you’re getting paid monthly, but is it actually profitable? Learn to calculate your hourly overhead burden so you can master your IT services pricing strategy.

FordraidersAuthor Commented:
Et All,

was not expecting this but:
Msg 8134, Level 16, State 1, Line 18
Divide by zero error encountered.
Warning: Null value is eliminated by an aggregate or other SET operation.
Bill PrewIT / Software Engineering ConsultantCommented:
You can check for divide by zero using a CASE statement and avoid the error that way.  In the example below if we would divide by zero I make the field be 0, otherwise do the calculation.  Change the 0 to whatever makes sense for you.

CASE DATEDIFF(MONTH, CR1.Start_Date, getdate()) WHEN 0 THEN 0 ELSE (ctct.[sls_total] / DATEDIFF(MONTH, CR1.Start_Date, getdate())) * DATEDIFF(MONTH, CR1.Start_Date, CR1.End_Date) END AS  "Sales_Projection_Thru_end_of_term"

Open in new window



»bp
FordraidersAuthor Commented:
thanks all  appreciate the help !!
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that helped.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.