Oracle SQL 按日期查找唯一 ID
Posted
技术标签:
【中文标题】Oracle SQL 按日期查找唯一 ID【英文标题】:Oracle SQL find unique id by date 【发布时间】:2019-10-22 18:46:11 【问题描述】:专家您好,
我在 Windows 上运行 Oracle 12c。 我进行了查询以查找任何新 ID 和输入日期。我想知道如何更改查询以在搜索中包含确切的日期,而不是仅仅扫描表上的所有日期记录。 我的查询:
select carid, inventory_date from car_source t where not exists
(select carid from car_source t2 where t.carid = t2.carid and t.inventory_date != t2.inventory_date);
表构建:
CREATE TABLE "CAR_SOURCE"
( "COUNT_CARS" NUMBER(10,0),
"EQUIPMENT_TYPE" VARCHAR2(100 BYTE),
"LOCATION" VARCHAR2(500 BYTE),
"INVENTORY_DATE" DATE,
"COUNT_INVENTORY" VARCHAR2(3000 BYTE),
"CARID" VARCHAR2(20 BYTE)
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1
BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "USERS" ;
插入行:
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (3,'Trans AM','London',to_date('04-JUL-19','DD-MON-RR'),null,'0001');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (5,'Chevy Volt','London',to_date('30-AUG-19','DD-MON-RR'),null,'0002');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (6,'Trans AM','coventry',to_date('07-SEP-19','DD-MON-RR'),null,'0001');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (5,'Chevy Volt','coventry',to_date('02-SEP-19','DD-MON-RR'),null,'0002');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (8,'Rovers','coventry',to_date('07-SEP-19','DD-MON-RR'),null,'0003');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (9,'Jaguars','coventry',to_date('07-SEP-19','DD-MON-RR'),null,'0004');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (7,'Rovers','London',to_date('30-AUG-19','DD-MON-RR'),null,'0003');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (10,'Escalade','London',to_date('17-SEP-19','DD-MON-RR'),null,'0005');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (12,'Mustangs','London',to_date('11-SEP-19','DD-MON-RR'),null,'0006');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (2,'Trans AM','Brummy',to_date('20-SEP-19','DD-MON-RR'),null,'0001');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (5,'Limo','London',to_date('12-SEP-19','DD-MON-RR'),null,'0007');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (2,'Lambo','Essex',to_date('19-SEP-19','DD-MON-RR'),null,'0008');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (1,'Limo','London',to_date('19-SEP-19','DD-MON-RR'),null,'0007');
Insert into CAR_SOURCE (COUNT_CARS,EQUIPMENT_TYPE,LOCATION,INVENTORY_DATE,COUNT_INVENTORY,CARID) values (2,'Chevy Volt','London',to_date('19-SEP-19','DD-MON-RR'),null,'0002');
结果:
卡片日期 0004 07-SEP-19 0005 17-SEP-19 0006 11-SEP-19 0008 19-SEP-19假设我想查询特定日期“19-SEP-19”,我会为第一个查询创建一个别名并将其连接到第二个日期查询吗? 任何有关如何将其整合在一起的建议都会非常有帮助。 最亲切的问候, 吉利
【问题讨论】:
样本数据和期望的结果会有所帮助。 【参考方案1】:如果要查找在特定日期输入的 id,可以使用聚合:
select car_id
from car_source
group by car_id
having min(inventory_date) = date '2019-09-19';
EXISTS
如果您想要表中的所有记录,这很有用。但是,如果您只想在该日期首次输入 car_id
s,那么聚合是一种简单的方法。
【讨论】:
谢谢你,Gordon,我希望在特定的日子里发现任何独特的(新的 id),这些都是以前在桌子上不存在的。 啊,再次感谢戈登。我知道我找不到我正在寻找的结果,我需要扫描整个表以获取新的 id。我的逻辑无法从仅 1 天开始收集新的 id - 我需要扫描所有记录以确保 id 是新的。 @Gilly 。 . .这就是这段代码的作用。它返回在指定日期最先出现的 id。以上是关于Oracle SQL 按日期查找唯一 ID的主要内容,如果未能解决你的问题,请参考以下文章