首页 > 解决方案 > 在 Oracle SQL 中选择带有子查询的查询

问题描述

这是我试图通过大量简化来实现的一个例子。

我有一张这样的桌子:

CREATE TABLE temp_pt
    (
        pt_key          number PRIMARY KEY
        , history       VARCHAR(20)
        , country       VARCHAR(2)
        , currency      VARCHAR(3)
        , settlementday VARCHAR(10)
    );

这张表中有一些记录,说如下:

insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(1,  'MATCH', 'GB', 'GBP', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(2,  'MATCH', 'GB', 'GBP', '2021-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(3,  'MATCH', 'GB', 'EUR', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(4,  'MATCH', 'GB', 'EUR', '2021-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(5,  'MATCH', 'GI', 'GBP', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(6,  'MATCH', 'GI', 'GBP', '2021-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(7,  'MATCH', 'GI', 'EUR', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(8,  'MATCH', 'GI', 'EUR', '2021-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(9,  'MATCH', 'NL', 'GBP', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(10, 'MATCH', 'NL', 'GBP', '2021-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(11, 'MATCH', 'NL', 'EUR', '2020-12-01');
insert into  temp_pt(PT_KEY, history, country, currency, settlementday) values(12, 'MATCH', 'NL', 'EUR', '2021-12-01');

我需要结算日 > 2020-12-31 的所有记录,除了那些(货币是 GBP,国家是 GB 或 GI)。我怎么写这个?

标签: sqloracleselectsubquery

解决方案


需要选择此表中货币为 GBP 的所有记录,但如果 country 为 GB 或 GI 则不需要。

这似乎是一个简单的where子句:

select t.*
from temp_pt
where current = 'GBP' and country not in ('GB', 'GI')

推荐阅读