从表中选择不同的记录

时间:2019-05-08 19:19:50

标签: sql oracle distinct

我想通过从下图选择数据集来编写查询 数据集来自migration_data_temp table

enter image description here

我希望数据像下面的图像一样,它将填充实际的表migration_data,请注意,即使名称是migration_data_temp而不是GTT,也应将其视为暂存桌子

enter image description here

我尝试了分析功能,但是没有运气,我也尝试了SELECT Distinct选项。

表格脚本

CREATE TABLE RIM_API_USER.MIGRATION_DATA_TEMP
(
  ID             NUMBER         GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  NAME           VARCHAR2(250 BYTE),
  TEMPLATE_NAME  VARCHAR2(250 BYTE),
  PARENT_NAME    VARCHAR2(250 BYTE),
  PORT_OF_ENTRY  VARCHAR2(100 BYTE)
)


Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME)Values (1, 'TN - People - 2019- Mode of Migration', 'Mode of Migration Template');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(2, '2 Migration', '2 Migration', 'TN - People - 2019- Mode of Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(3, '2.2 Mode of Migration', '2.2 Mode of Migration', '2 Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(4, '2.2.T Transportation', '2.2.T Transportation', '2.2 Mode of Migration', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values (5, '2.2.T.1 Air', '2.2.T.1 Air', '2.2.T Transportation', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(6, '2.2.T.1.1 American Airways', '2.2.T.1.1 American Airways', '2.2.T.1 Air', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(7, '2.2.T.1.2 Southwest Airlines', '2.2.T.1.2 Southwest Airlines', '2.2.T.1 Air', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(8, '2.2.T.1.3 Spirit Airlines', '2.2.T.1.3 Spirit Airlines', '2.2.T.1 Air', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(9, '2.2.T.1.4 Allegiant Airlines', '2.2.T.1.4 Allegiant Airlines', '2.2.T.1 Air', 'NY');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(10, '2 Migration', '2 Migration', 'TN - People - 2019- Mode of Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(11, '2.2 Mode of Migration', '2.2 Mode of Migration', '2 Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(12, '2.2.T.2 Rail', '2.2.T.2 Rail', '2.2.T Transportation', 'IL');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(13, '2.2.T.2.1 Amtrak', '2.2.T.2.1 Amtrak', '2.2.T.2 Rail', 'IL');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(14, '2.2.T.2.2 Metro Train', '2.2.T.2.2 Metro Train', '2.2.T.2 Rail', 'IL');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(15, '2.2.T.2.3 Path Transfer', '2.2.T.2.3 Path Transfer', '2.2.T.2 Rail', 'IL');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(16, '2 Migration', '2 Migration', 'TN - People - 2019- Mode of Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(17, '2.2 Mode of Migration', '2.2 Mode of Migration', '2 Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(18, '2.2.T.3 Road', '2.2.T.3 Road', '2.2.T Transportation', 'CA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(19, '2.2.T.3.1 Car', '2.2.T.3.1 Car', '2.2.T.3 Road', 'CA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(20, '2.2.T.3.2 Bike', '2.2.T.3.2 Bike', '2.2.T.3 Road', 'CA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(21, '2.2.T.3.3 Cycle', '2.2.T.3.3 Cycle', '2.2.T.3 Road', 'CA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(22, '2.2.T.3.4 Walking', '2.2.T.3.4 Walking', '2.2.T.3 Road', 'CA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(23, '2 Migration', '2 Migration', 'TN - People - 2019- Mode of Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME)Values(24, '2.2 Mode of Migration', '2.2 Mode of Migration', '2 Migration');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(25, '2.2.T.4 Sea', '2.2.T.4 Sea', '2.2.T Transportation', 'VA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(26, '2.2.T.4.1 Boat', '2.2.T.4.1 Boat', '2.2.T.4 Sea', 'VA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(27, '2.2.T.4.2  Ship', '2.2.T.4.2  Ship', '2.2.T.4 Sea', 'VA');
Insert into RIM_API_USER.MIGRATION_DATA_TEMP(ID, NAME, TEMPLATE_NAME, PARENT_NAME, PORT_OF_ENTRY)Values(28, '2.2.T.4.3 yacht', '2.2.T.4.3 yacht', '2.2.T.4 Sea', 'VA');
COMMIT;

2 个答案:

答案 0 :(得分:0)

我猜对了吗?是否要仅通过在一个列上应用不重复来选择多个列?如果是,则here是解决方案。如果没有,请准确告诉我

答案 1 :(得分:0)

请尝试以下操作:

select a.id, a.name, a.template_name, a.parent_name, a.port_of_entry
from (
select t.*, 
row_number() over (partition by t.name, t.template_name, t.parent_name order by id) rnk
from migration_data_temp t )a
where a.rnk = 1
order by a.id

测试结果:

DB<>Fiddle