Oracle 查询计算与日期数组完全相同的匹配项
Oracle query to count exactly same match with array of dates
Oracle 数据库中有以下两个表。连接是1:n,所以一个商家可以有很多订单。
现在我必须编写一个 SQL 语句,检查给定名称下的企业是否存在,其中有预定义的订单日期。
示例:
我们有名为“B1”的公司,该公司有两个 'Orders',分别为“7/4/2020”和“3/1/2021”。
比方说,我有一个名称“B1”和一个输入集('7/4/2020'、'8/1/2021'),它应该 return 表明“B1 与这些订单日期不完全相同”存在”。但是使用名称“B1”和输入集('3/1/2021','7/4/2020')returns“具有完全这些订单日期的 B1 已经存在”。
我的问题:我找不到合适的语法。如何在“IN”运算符的输入集中使用日期?最有效的想法是什么?
如果您总是比较两个订单日期,那么试试这个:
架构和插入语句:
create table business_data (id int ,name varchar(50));
CREATE TABLE ORDERS (ID INT, ORDERDATE DATE, BUSINESS_ID INT);
INSERT INTO BUSINESS_DATA VALUES (1,'B1');
INSERT INTO orders VALUES (1,'3-jan-2021',1);
INSERT INTO orders VALUES (2,'7-apr-2020',1);
INSERT INTO orders VALUES (3,'7-may-2020',1);
查询#1(具有匹配的订单日期)
select (case when count(*)>0 then (max(name)||' with exactly these order dates already exist') else (max(name)||' with exactly these order dates not exist') end)as results from business_data b
where b.name='B1' and exists
(
select 1 from orders o where b.id=o.business_id and o.orderdate = any ('3-jan-2021', '7-apr-2020')
group by business_id
having count(distinct orderdate)>=2
)
RESULTS
B1 with exactly these order dates already exist
查询#2(订单日期不匹配)
select (case when count(*)>0 then (max(name)||' with exactly these order dates already exist') else (max(name)||' with exactly these order dates not exist') end)as results from business_data b
where b.name='B1' and exists
(
select 1 from orders o where b.id=o.business_id and o.orderdate = any ('3-jan-2021', '8-apr-2120')
group by business_id
having count(distinct orderdate)>=2
)
输出:
RESULTS
with exactly these order dates not exist
db<>fiddle here
Oracle 数据库中有以下两个表。连接是1:n,所以一个商家可以有很多订单。
现在我必须编写一个 SQL 语句,检查给定名称下的企业是否存在,其中有预定义的订单日期。
示例: 我们有名为“B1”的公司,该公司有两个 'Orders',分别为“7/4/2020”和“3/1/2021”。 比方说,我有一个名称“B1”和一个输入集('7/4/2020'、'8/1/2021'),它应该 return 表明“B1 与这些订单日期不完全相同”存在”。但是使用名称“B1”和输入集('3/1/2021','7/4/2020')returns“具有完全这些订单日期的 B1 已经存在”。
我的问题:我找不到合适的语法。如何在“IN”运算符的输入集中使用日期?最有效的想法是什么?
如果您总是比较两个订单日期,那么试试这个:
架构和插入语句:
create table business_data (id int ,name varchar(50));
CREATE TABLE ORDERS (ID INT, ORDERDATE DATE, BUSINESS_ID INT);
INSERT INTO BUSINESS_DATA VALUES (1,'B1');
INSERT INTO orders VALUES (1,'3-jan-2021',1);
INSERT INTO orders VALUES (2,'7-apr-2020',1);
INSERT INTO orders VALUES (3,'7-may-2020',1);
查询#1(具有匹配的订单日期)
select (case when count(*)>0 then (max(name)||' with exactly these order dates already exist') else (max(name)||' with exactly these order dates not exist') end)as results from business_data b
where b.name='B1' and exists
(
select 1 from orders o where b.id=o.business_id and o.orderdate = any ('3-jan-2021', '7-apr-2020')
group by business_id
having count(distinct orderdate)>=2
)
RESULTS |
---|
B1 with exactly these order dates already exist |
查询#2(订单日期不匹配)
select (case when count(*)>0 then (max(name)||' with exactly these order dates already exist') else (max(name)||' with exactly these order dates not exist') end)as results from business_data b
where b.name='B1' and exists
(
select 1 from orders o where b.id=o.business_id and o.orderdate = any ('3-jan-2021', '8-apr-2120')
group by business_id
having count(distinct orderdate)>=2
)
输出:
RESULTS |
---|
with exactly these order dates not exist |
db<>fiddle here