Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It seems like Scalar Subquery is not available in AlaSQL. #1967

Open
hotsun1508 opened this issue Aug 31, 2024 · 0 comments
Open

It seems like Scalar Subquery is not available in AlaSQL. #1967

hotsun1508 opened this issue Aug 31, 2024 · 0 comments

Comments

@hotsun1508
Copy link

hotsun1508 commented Aug 31, 2024

Hi, I thought sub query was working in AlaSQL(https://github.com/AlaSQL/alasql/wiki/Subqueries) however, this scalar subquery seems like it's not working.

  • Query Explanation: This query calculates the average profit per customer by education level and annual income, and compares it with the previous period. It assumes that "previous period" means the previous stock period.
<SQL-99>
SELECT 
    [educationlevel],
    [annualincome],
    AVG([totalprofit]) AS [average_profit_current_period],
    (SELECT AVG([totalprofit]) 
     FROM [temptable] AS previous
     WHERE previous.[educationlevel] = current.[educationlevel]
       AND previous.[annualincome] = current.[annualincome]
       AND previous.[stockperiod] = current.[stockperiod] - 1) AS [average_profit_previous_period]
FROM 
    [temptable] AS current
WHERE 
    [educationlevel] IS NOT NULL 
    AND [annualincome] IS NOT NULL 
    AND [totalprofit] IS NOT NULL
GROUP BY 
    [educationlevel], [annualincome]
ORDER BY 
    [educationlevel], [annualincome];

This is the Error Message I'm facing.

runQuery.js:25 Failed to execute query: 
TypeError: Cannot read properties of undefined (reading 'educationlevel')
    at pr.eval [as wherefn] (eval at T.Select.compileWhere (alasql@4:78:9531), <anonymous>:3:76)
    at Wt (alasql@4:60:6110)
    at Wt (alasql@4:60:7141)
    at Er (alasql@4:59:1015)
    at br (alasql@4:59:926)
    at l.options.autocommit.l.databases.<computed>.engineid.l.databases.<computed>.tables.<computed>.view.s.datafn (alasql@4:78:2021)
    at alasql@4:59:429
    at Array.forEach (<anonymous>)
    at Ts (alasql@4:59:389)
    at Array.u (alasql@4:77:648)

Just to make sure that the query itself has no issue, I've tried to run it in PostgreSQL, indeed I got a decent result.

SELECT 
    educationlevel,
    annualincome,
    AVG(totalprofit) AS average_profit_current_period,
    (SELECT AVG(previous.totalprofit) 
     FROM adventureworks."adventureworks-total" AS previous
     WHERE previous.educationlevel = current.educationlevel
    ) AS average_profit_previous_period
FROM 
    adventureworks."adventureworks-total" AS current
WHERE 
    educationlevel IS NOT NULL 
    AND annualincome IS NOT NULL 
    AND totalprofit IS NOT NULL
GROUP BY 
    educationlevel, annualincome
ORDER BY 
    educationlevel, annualincome;

Following is the screenshot of 5 head rows from the result table.

result

Please help me resolve this error.
Thank you so much :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants