sql server - Get all parent rows that do not have a row for current date in child table? -
[Mission] [Mission Id] FOR [DBO]. [Mission] Join the left OUTER [DBO]. [DBO] on [report] [Mission]. [Mission id] = [dbo]. [Report]. [Mission Id] WHERE [DBO] [Report]. There is a tap order by the date of the report [DBO]. [ Mission]. [MISSION ID]
How can I change the above question, as it gives me the MissionId
from the table [dBO] '. [Mission]
There is no line in that table [dbo]. [Report]
where [dbo]. [Report]. Report date is
today?
Mission ID
is the primary key in the table mission
and report a foreign key
in the table. That's why I want to receive all the missions who do not have a line in the table for the current date in the report
.
I have introduced some aliases to make the query easier to read, and added the necessary condition I have also changed the WHERE section
, not sure if it is required:
SELECT m [mission id] from FROM [dbo] [Mission] M LEFT OUTER JOIN [DBO]. [Report] R. M [MissionId] = R [Mission Id] and R. Date of report = DATEADD (day, date (day, 0, gateway), 0) where r. MISIIDID has given the Nulled Order. [Mission ID]
It assumes that the date of the ReportDate
happens with the scheduled time, if not, then a little more complex question is required: / P>
SELECT m [Mission id] from FROM [dbo] [Mission] M does not exist (choose from dbo.Report r *) where r.MissionID = m.MissionID and r.ReportDate & gt; = DATEADD (day, DATEDIFF (day, 0, GETDATE ()), 0) and airportport & date; DATEADD (DAY, DATEDIFF (DAY, 0, GETDATE ()), 1)) By order [Mission ID]
gives the current date and time I am using a few eras and I take that value and convert it to a current date in midnight, and (in the second query) tomorrow night at midnight.
The second query is fully runable query:
Allow the table to be declared (MissionIndIntIP); Insert in Entry (Mission ID) 1 Select Union Select All 2; Announce @Repport Table (MissionIndItNull, ReportDateDateTime is not blank); Insert in the @report (mission id, report date) 2, GETDATE () Select Union Select all 1, DATEAD (day, -1, GETDATE ()); SELECT M. [Mission Id] was not entered before (select * Report R from *) r.MissionID = m.MissionID and r.ReportDate & gt; = DATEAD (day, DATEDIFF (day, 0, GETDATE ()), 0) and Rportport & Lt; DATEAD (Day, DATEDIFF (Day, 0, GETDATE ()), 1)) By order [Mission ID]
Result:
Mission Id - --------- 1