We help IT Professionals succeed at work.

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

57 Views
Last Modified: 2019-02-12
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

CERTIFIED EXPERT

Commented:
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 SData Engineer
CERTIFIED EXPERT

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
CERTIFIED EXPERT
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
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 PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Top Expert 2016

Commented:
Welcome, glad that helped.


»bp

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.