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

Fordraiders
Fordraiders used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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 Engineer

Commented:
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

Data Engineer
Commented:
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

Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

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 Consultant
Top Expert 2016

Commented:
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

Author

Commented:
thanks all  appreciate the help !!
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Welcome, glad that helped.


»bp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial