In the Index function on APP.PY of my finance code, I'm getting "RuntimeError: no such column: total_per_share" even though I'm using "AS" to create this column -- please advise.

def index():
    userid = session["user_id"]
    """Show portfolio of stocks"""
    # Calculate the sum cash value of each share holding and update it in PORTFOLIO
    db.execute("SELECT symbol, shares, price, (shares * price) AS total_per_share, sum(total_per_share) AS total_share FROM portfolio WHERE id = ? GROUP BY symbol", userid)
    cash_dict = db.execute("SELECT cash FROM users WHERE id = ?", session["user_id"])
    owned_stocks = db.execute("SELECT symbol, total_share, name, sum(shares) AS sum_shares, price FROM portfolio WHERE id = ? GROUP BY symbol", userid)
    cash = cash_dict[0]["cash"]

1 Answer 1


SQLite (or any other flavor of SQL) does not allow using column aliases in the same SELECT query.

  • Got it! Thank you :-) Mar 8, 2022 at 22:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .