Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6193117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:03:46+00:00 2026-05-24T03:03:46+00:00

Suppose equity has a column called TickerID. I would like to replace the 111’s

  • 0

Suppose equity has a column called TickerID. I would like to replace the 111’s with equity.TickerID. MySQL can’t seem to resolve the scope and returns an unknown column when I try that. This SQL statement works but I need to run it for each ticker. Would be nice if I could get a full table.

SELECT Ticker,
    IF(tbl_m200.MA200_Count = 200,tbl_m200.MA200,-1) AS MA200,
    IF(tbl_m50.MA50_Count = 50,tbl_m50.MA50,-1) AS MA50,
    IF(tbl_m20.MA20_Count = 20,tbl_m20.MA20,-1) AS MA20
FROM equity
INNER JOIN 
(SELECT  TickerID,AVG(Y.Close) AS MA200,COUNT(Y.Close) AS MA200_Count FROM 
(
    SELECT Close,TickerID FROM equity_pricehistory_daily
    WHERE TickerID = 111
    ORDER BY Timestamp DESC LIMIT 0,200
) AS Y
) AS tbl_m200
USING(TickerID)

INNER JOIN 
(SELECT  TickerID,AVG(Y.Close) AS MA50,COUNT(Y.Close)  AS MA50_Count FROM 
(
    SELECT Close,TickerID FROM equity_pricehistory_daily
    WHERE TickerID = 111
    ORDER BY Timestamp DESC LIMIT 50
) AS Y
) AS tbl_m50
USING(TickerID)

INNER JOIN 
(SELECT  TickerID,AVG(Y.Close) AS MA20,COUNT(Y.Close) AS MA20_Count FROM 
(
    SELECT Close,TickerID FROM equity_pricehistory_daily
    WHERE TickerID = 111
    ORDER BY Timestamp DESC LIMIT 0,20
) AS Y
) AS tbl_m20
USING(TickerID)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:03:47+00:00Added an answer on May 24, 2026 at 3:03 am

    This seems to be some bug or “feature” of MySQL. Many persons seems to have the same problem with outer tables being out of scope.

    Anyway… You could create functions that retrieve the information you want:

    DROP FUNCTION IF EXISTS AveragePriceHistory_20;
    
    CREATE FUNCTION AveragePriceHistory_20(MyTickerID INT)
    RETURNS DECIMAL(9,2) DETERMINISTIC
    RETURN (
        SELECT AVG(Y.Close)
        FROM (
            SELECT Z.Close
            FROM equity_pricehistory_daily Z
            WHERE Z.TickerID = MyTickerID
            ORDER BY Timestamp DESC
            LIMIT 20
        ) Y
        HAVING COUNT(*) = 20
    );
    
    SELECT
        E.TickerID,
        E.Ticker,
        AveragePriceHistory_20(E.TickerID) AS MA20
    FROM equity E;
    

    You would get NULL instead of -1. If this is undesirable, you could wrap the function-call with IFNULL(...,-1).


    Another way of solving this, would be to select for the time-frame, instead of using LIMIT.

    SELECT
        E.TickerID,
        E.Ticker,
        (
            SELECT AVG(Y.Close)
            FROM equity_pricehistory_daily Y
            WHERE Y.TickerID = E.TickerID
            AND Y.Timestamp > ADDDATE(CURRENT_TIMESTAMP, INTERVAL -20 DAY)
        ) AS MA20
    FROM equity E;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose your git history looks like this: 1 2 3 4 5 1–5 are
Suppose I have: Toby Tiny Tory Tily Is there an algorithm that can easily
Suppose you're developing a software product that has periodic releases. What are the best
suppose a class has private data members but the setters and getters are in
Suppose I need to create my own small DSL that would use Python to
Suppose I have a grid of squared defined like so in a class: Square
Suppose I have a innerclass extends asynctask called A and the outerclass extends Activity
Suppose I have a struct called Node as follows: struct foo { foo *next;
Suppose that I have an n -sided loaded die, where each side k has
Suppose i have an XML file, that i use as local database, like this):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.