SQL需要修复/修改/完成代码的帮助

时间:2019-05-11 19:47:00

标签: sql oracle

我正在努力解决我的代码练习解决方案的问题。我认为我启动得很好并且有很好的基础,但是它不会运行(出现无效的标识符错误)。也许我的代码太乱了还是没有意义?不确定。如果您能想到一种更好的方法来解决此代码解决方案/回答查询,请告诉我。

我正在使用Oracle SQL Developer。问题和ERD如下。预先谢谢你

  

对于每个证券交易所,显示总交易份额最高的股票代码。显示证券交易所名称,股票代码和总交易份额。按证券交易所名称和股票代号对输出进行排序。

WITH HighestTradeVolume   
    (SELECT COUNT(trade.shares) total_trades,
            stock_listing.stock_symbol AS "Stock symbol",       
            stock_exchange.symbol AS "Stock Exchange Symbol",          
            stock_exchange.name AS "Stock Exchange"    
    FROM stock_exchange       
         JOIN trade    
              ON trade.stock_ex_id = stock_exchange.stock_ex_id    
         JOIN stock_listing
              ON stock_listing.stock_id = trade.stock_id
              AND stock_listing.stock_ex_id = trade.stock_ex_id     
    GROUP BY stock_exchange.name, stock_exchange.symbol, stock_listing.stock_symbol
        ORDER BY stock_exchange.name, stock_exchange.symbol, stock_listing.stock_symbol)    
    HAVING SUM(trade.shares) = (SELECT MAX(SUM(trade.shares))    
                                 FROM trade );

ERD

1 个答案:

答案 0 :(得分:0)

您在ORDER BY行的另一边有一个括号,在结尾处也缺少了括号。

ORDER BY stock_exchange.name, stock_exchange.symbol, stock_listing.stock_symbol --) Extra   
HAVING SUM(trade.shares) = (SELECT SUM(trade.shares) FROM trade ) --Applying MAX is not required.
) --Missing
;