如何找到特定查询在表中没有返回行的最新日期?

问题描述 投票:0回答:1

我有一个 SQL 查询:

select max(date) 
    from index_constituents 
    where (opening_closing ='O' 
        and index_code ='buk350n' 
        and issuer = 'cboe') 

我相信它会返回我的

index_constituents
表中的最新日期。

我正在尝试查找没有返回行的最新日期。

表格

index_constituents
看起来像:

id 日期 发行人 索引代码 开幕_闭幕
1393 2016-11-25 芝加哥期权交易所 buk350n O
1394 2016-11-25 芝加哥期权交易所 buk350n O
1395 2016-11-25 芝加哥期权交易所 buk350n O
1402 2016-11-25 芝加哥期权交易所 buk350n O

我遇到的问题是,如果我的查询返回没有数据的一天,则也不会存在可以从中提取

max(date)
的日期。

任何帮助都会很棒!

sql postgresql dbeaver
1个回答
0
投票

工作正常。您可能对列的数据类型或值感到困惑:

CREATE TABLE index_constituents (id int, date date, issuer varchar(10),
  index_code varchar(255), opening_closing varchar(20));

INSERT INTO index_constituents VALUES
  (1393, '2016-11-25', 'cboe', 'buk350n', 'O'),
  (1394, '2016-11-25', 'cboe', 'buk350n', 'O'),
  (1395, '2016-11-25', 'cboe', 'buk350n', 'O'),
  (1402, '2016-11-25', 'cboe', 'buk350n', 'O');
select max(date)
    from index_constituents 
    where (opening_closing = 'O'
        and index_code ='buk350n' 
        and issuer = 'cboe'); 

用于输出检查:https://dbfiddle.uk/8FNVcK-l

© www.soinside.com 2019 - 2024. All rights reserved.