任何人都可以想到此查询的另一种解决方案(或者,如果看到这种错误,则可以解决我的错误吗?我的解决方案显然没有给我正确的结果(那是我的教授说的),并且他不会告诉我为什么。感谢您的帮助。问题,ERD,输入和我的输出如下:
输出:
2 Euronext Paris EP 2019-04-22 13:06:36
1 London Stock Exchange LSE 2019-03-28 13:06:36
5 Moscow Stock Exchange MSE NULL
6 NASDAQ Stock Exchange NASDAQ 2019-05-09 13:06:36
3 New York Stock Exchange NYSE 2019-05-20 13:06:36
4 Tokyo Stock Exchange TSE 2019-04-10 13:06:36
- 对于每个证券交易所列出的每个股票,显示交易所名称, 股票代码以及该股票最后交易的日期和时间。按证券交易所名称,股票代号对输出进行排序。如果尚未交易股票,请在上次交易日期显示NULL。
SELECT
stock_exchange.stock_ex_id,
stock_exchange.name stock_exchange_name,
stock_exchange.symbol,
nvl(to_char(max(trade.transaction_time), 'yyyy-mm-dd hh24:mi:ss'), 'NULL') as last_traded
FROM stock_exchange
LEFT JOIN trade
ON trade.stock_ex_id = stock_exchange.stock_ex_id
GROUP BY stock_exchange.stock_ex_id, stock_exchange.name, stock_exchange.symbol
ORDER BY stock_exchange.name, stock_exchange.symbol;
答案 0 :(得分:1)
您的ERD缺少Trade和Stock_Listing之间的关系。在Stock_Listing中可以获取股票代码:
SELECT
stock_exchange.stock_ex_id,
stock_exchange.name stock_exchange_name,
stock_listing.symbol,
nvl(to_char(max(trade.transaction_time), 'yyyy-mm-dd hh24:mi:ss'), 'NULL') as last_traded
FROM stock_exchange
JOIN stock_listing
ON stock_exchange.stock_ex_id = stock_listing.stock_ex_id
LEFT JOIN trade
ON trade.stock_ex_id = stock_listing.stock_ex_id
and trade.stock_id = stock_listing.stock_id
GROUP BY stock_exchange.stock_ex_id, stock_exchange.name, stock_listing.symbol
ORDER BY stock_exchange.name, stock_listing.symbol;
注意:
stock_ex_id
NULL
。空格(null
)就够了吗?答案 1 :(得分:0)
我想你想要
select sl.stock_ex_id, se.name, sl.symbol, max(t.trade_time)
from stock_listing sl join -- inner join should be fine
stock_exchange se
on se.stock_ex_id = sl.stock_ex_id left join
trade t
on t.stock_id = sl.stock_id
group by sl.stock_ex_id, se.name, sl.symbol;
该问题的措辞是“针对每个股票清单”,因此在我看来,从该表开始并保留其所有行是有意义的。
max()
将返回NULL
的值而不是字符串'NULL'
,但这似乎与问题相符。