Avatar of Fordraiders
Fordraiders
Flag for United States of America asked on

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
Microsoft SQL ServerSQL

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
OMC2000

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

Sharath S

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

ASKER CERTIFIED SOLUTION
Sharath S

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Fordraiders

ASKER
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.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Bill Prew

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
Fordraiders

ASKER
thanks all  appreciate the help !!
Bill Prew

Welcome, glad that helped.


»bp
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.