Need help joining multiple tables to give the maximum number for each one?

浪尽此生 提交于 2020-12-15 05:27:58

问题


I'm working on a project and need to create a few join statements. I tried to make one that selects the maximum value of a column in three different tables. I'm getting an error on the SELECT that says "select is not valid at this server position, expecting FOR, LOCK, TABLE, VALUES, WITH, '('". Not sure what to do, any advice? Code is below.

SELECT MAX(cn.TotalDollarPerArea) as 'North Total Dollar per Area',
        MAX(cs.TotalDollarPerArea) as 'South Total Dollar per Area', 
        MAX(ncl.TotalDollarPerArea) as 'Non-Climate Total Dollar Per Area',
FROM ClimateNorth cn,
    JOIN ClimateSouth cs using (TotalDollarPerArea),
    JOIN NonClimate ncl using (TotalDollarPerArea);

回答1:


Just use subqueries:

SELECT (SELECT MAX(cn.TotalDollarPerArea)
        FROM ClimateNorth cn
       ) as `North Total Dollar per Area`,
       (SELECT MAX(cs.TotalDollarPerArea)
        FROM ClimateSouth cs
       ) as `South Total Dollar per Area`,
       (SELECT MAX(ncl.TotalDollarPerArea)
        FROM NonClimate ncl
       ) as `Non-Climate Total Dollar Per Area`;

Note: Only use single quotes for string and date constants. There are better escape characters for column aliases.



来源:https://stackoverflow.com/questions/65172090/need-help-joining-multiple-tables-to-give-the-maximum-number-for-each-one

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!