Requirement: query the information input by the user when the user is not on a business trip: subquery: We can explicitly query the user IDs that enter information during the business trip, and
// Requirement: // query the information entered when the user is not on a business trip // analysis: // subquery: We can explicitly query the user IDs that have entered the information during the business trip, and
// Requirement:
// Query the information entered when the user is not on a business trip
// Analysis:
// Subquery: We can explicitly query the user ID, time, and
// Master query: we retrieve information from the user list, provided that the user id and write_date are not found in the subquery id and time
//
// User list
With tmp_a (
Select 1 id, to_date ('20170101', 'yyyymmdd') write_date, 'tttttttt1' text from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt2' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt3' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt4' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt5' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt6' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt7' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt8' from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), 'tttttt9' from dual union all
Select 1, to_date ('20140901', 'yyyymmdd'), 'tttttt10' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss1' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss2' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss3' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss4' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss5' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss6' from dual union all
Select 2, to_date ('20140901', 'yyyymmdd'), 'ssssss7' from dual)
// Travel record table
, Tmp_ B (
Select 1 id, to_date ('20140901', 'yyyymmdd') begin_dt, to_date ('20140901', 'yyyymmdd') end_dt from dual union all
Select 1, to_date ('20170101', 'yyyymmdd'), to_date ('20170101', 'yyyymmdd') from dual union all
Select 2, to_date ('20170101', 'yyyymmdd'), to_date ('20170101', 'yyyymmdd') from dual)
// Query SQL
// The problem to be solved
// 1. For the same user, do not use not between for separate business trips (different start times exist ).
// 2. Some users may not be able to simply use a. id = B. id without a travel record
// Filter (not in) to query user details without travel records
Select s. id, s. write_date, s. text
From tmp_a s
Where (s. id, s. write_date) not in
(// User details with travel records
Select distinct a. id, a. write_date
From tmp_a inner join tmp_ B B
On a. id = B. id
Where a. write_date
Between B. begin_dt and B. end_dt
)
ID WRITE_DATE TEXT
-----------------------------
1 2010-04-05 tttttt1
1 2010-04-06 tttttt2
1 2010-04-10 tttttt6
1 2010-04-13 tttttt9
1 2010-04-14 tttttt10
2 2010-04-05 ssssss1
2 2010-04-06 ssssss2
2 2010-04-07 ssssss3
2 2010-04-11 ssssss7