sql server 2008 - SQL query to display records with no related record within a specific date range -


sql server 2008 r2 have 2 tables. 1 main records , other notes related main common key field sysid , main_id. (there can many notes 1 main record)

i want list records in main table not have record created in notes table in last 30 days related record.

main table fields > file_number, sysid

notes table fields > note_date, main_id

i have tried hundred different suggestions searching , nothing gives me need. think below closest gives records no note exists @ , dont see main records have existing notes dont have in last 30 days. same results if take out and db.notes.[date] > dateadd(dd,-30,getdate())) line.

select *   db.main  not exists(select null        db.notes        db.main.sysid = db.notes.main_id , db.notes.[date] > dateadd(dd,-30,getdate()))  order db.main.ile_number 

any suggestions appreciated.

** client has asked additional column. last note date. want list of main records no notes in x days , date of last note.

i left outer join:

select *  db.main m left outer join db.notes n on m.sysid = n.main_id      , n.[date] > dateadd(dd, -30, getdate()))  n.main_id null order m.ile_number 

Comments

Popular posts from this blog

asp.net mvc 3 - Using mvc3, I need to add a username/password to the sql connection string at runtime -

kineticjs - draw multiple lines and delete individual line -

thumbnails - jQuery image rotate on hover -