ERD和代码练习问题如下。我遇到的问题是:
我不知道如何显示“如果尚未进行交易的股票在上次交易日期显示NULL。我尝试了nvl(transaction_time, 'NULL')
,但我认为那是不对的。
该练习要求最后一次交易股票的日期和时间,但是"transaction_time"
的外键仅显示日期。如何使用SQL代码访问时间?我是否要像SYSDATE?
我认为我有其他所有要解决的问题,但这只是两件事。我可能是错的。预先感谢
对于每个股票交易所中列出的每个股票,显示交易所名称,股票代码以及该股票的最后交易日期和时间。按证券交易所名称,股票代号对输出进行排序。如果尚未交易股票,请在上次交易日期显示NULL。
SELECT
stock_exchange.stock_ex_id,
stock_exchange.name stock_exchange_name,
stock_exchange.symbol,
trade.transaction_time
FROM stock_exchange
JOIN trade
ON trade.stock_ex_id = stock_exchange.stock_ex_id
ORDER BY stock_exchange.name, stock_exchange.symbol;
答案 0 :(得分:1)
使用外部联接来查找尚未交易的股票。 Oracle DATE数据类型包含一个时间元素,您只需要更改日期格式即可显示该时间元素:
SELECT stock_exchange.stock_ex_id,
stock_exchange.name stock_exchange_name,
stock_exchange.symbol,
to_char(max(trade.transaction_time), 'yyyy-mm-dd hh24:mi:ss') as last_traded_time
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;