How can I subtract two alias columns?


  • QA Engineer

    I need to find the difference between two columns that have an alias.

    The error I get is: "Error in Expression: Highest_Cost - Lowest_Cost" Invalid Column name: Highest_Cost

    Here is my query:

    select part,min(cost) as Lowest_Cost,max(cost) as Highest_Cost, min(date_received) as First_date, max(date_received) as Last_Date, description, Highest_Cost - Lowest_Cost as Difference
    from v_po_history 
    where part not like '*%' and date_received >= '2022-04-01' and date_received 


  • After googling for an hour I couldn't find an answer. I post this question and my next google search, I find the answer. basically you have to run the calculation again. you can't access alias' columns in the select statement they are declared.

    select part,min(cost) as Lowest_Cost,max(cost) as Highest_Cost, min(date_received) as First_date, max(date_received) as Last_Date, description, (max(cost) - min(cost)) as Difference
    from v_po_history 
    where part not like '*%' and date_received >= '2022-04-01' and date_received 


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2