Column 'discount' cannot be resolved. How to use a column that is the result of a calculation to calculate another column in the same query?

Asked

Viewed 44 times

2

I have the following query:

Select price, col2, col3*7.5 AS desconto, (price*desconto) AS finalprice
From tab1.

When I run I get the following message: Column 'desconto' cannot be resolved.

How to resolve this error?

Note: I’m running on AWS Athena following the same SQL standard.

  • Have you considered using a CTE ( http://www.macoratti.net/13/05/sql_cte1.htm ) to handle the columns before using?

  • What the SGBD used?

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

2

Based on your Query, it is not better to do it the way below?

Select price, col2, col3*7.5 AS desconto, (price*(col3*7.5)) AS finalprice
From tab1.

1

One option is to use a subquery in the FROM:

SELECT x.*,
       x.price * x.desconto AS finalprice
  FROM (
    SELECT price,
           col2,
           col3 * 7.5 AS desconto
      FROM tab1
  ) x

Browser other questions tagged

You are not signed in. Login or sign up in order to post.