sql server - Joining 2 SQL Queries from different tables -
i'm new sql apologies noob question. have searched other articles can't seem find works issue...
essentially have 2 tables (docmaster, doshistory), may have guessed query our dms. user wants report set of documents numbers (these template documents) following information; - created date - last accessed - last modified - document number (unique id)
i can above info 2 queries below;
created date + last modified date (from docmaster table)
select [docname],[docnum],[editwhen] 'last edited date',[entrywhen] 'created date' [knowledge_prod].[mhgroup].[docmaster] docnum in ('10098776', '1355264')
last accessed date (from dochistory table)
select docnum, max (activity_datetime) 'last accessed date' [knowledge_prod].[mhgroup].[dochistory] activity in ('view','create','copy', 'checkin', 'checkout','print','mail') , docnum in ('10098776', '1355264') group docnum
what best way join these queries based on docnum compile results like;
docnum - last accessed date - last edited date - created date
thankyou in advance assistance has been crushing soul last few hours. using smss querying.
since don't have test data test , below 1 safest way
;with cte ( select [docname],[docnum],[editwhen] 'last edited date',[entrywhen] 'created date' [knowledge_prod].[mhgroup].[docmaster] docnum in ('10098776', '1355264') ) ,cte1 ( select docnum, max (activity_datetime) 'last accessed date' [knowledge_prod].[mhgroup].[dochistory] activity in ('view','create','copy', 'checkin', 'checkout','print','mail') , docnum in ('10098776', '1355264') group docnum) select * cte c join cte1 c1 on c1.docnum=c.docnum
Comments
Post a Comment